How to kill setTimeout () function

I use the setTimeout () function through my application, but when its time to collect garbage. the method still works and calls the function. How to stop it from calling a specific function. I tried to set it to zero, but it does not work

+4
source share
5 answers

setTimeout returns a timeout link that you can use when you call clearTimeout .

var myTimeout = setTimeout(...); clearTimeout(myTimeout); 
+16
source

See: clearTimeout()

+2
source

Same. Use "clearInterval (timeoutInstance)".

If you are using AS3, I would use the Timer () class

 import flash.utils.*; var myTimer:Timer = new Timer(500); myTimer.addEventListener("timer", timedFunction); // Start the timer myTimer.start(); function timedFunction(e:TimerEvent) { //Stop timer e.target.stop(); } 
0
source

I had a similar situation, and it made me go crazy for a couple of hours. The answers I found on the net didn't help either, but in the end I found that calling System.gc() does the trick.

I use the weak listening operator ENTER_FRAME to check if the GC instance is being deleted. If the GC clears the object, ENTER_FRAME should stop working.

Here is an example:

 package { import flash.display.Sprite; import flash.events.Event; import flash.system.System; import flash.utils.getTimer; import flash.utils.setTimeout; public class GCTest { private var _sprite:Sprite; public function GCTest():void { this._sprite = new Sprite(); this._sprite.addEventListener(Event.ENTER_FRAME, this.test, false, 0, true); setTimeout(this.destroy, 1000); //TEST doesn't work } private function test(event:Event):void { trace("_" + getTimer()); //still in mem } public function destroy():void { trace("DESTROY") System.gc(); } }} 

When you comment out System.gc(); , the test method continues to receive a call even after the destroy method is called (so that the timeout is complete). This is probably due to the fact that there is still enough memory, so the GC does not push itself. When you comment on setTimeout, the test method will not be called at all, then setTimeout is definitely a problem.

Call System.gc(); will stop sending ENTER_FRAME.

I also did some tests with clearTimeout, setInterval and clearInterval, but this did not affect the GC.

Hope this helps some of you with the same or similar problems.

0
source

Nevermind, clearInterval ()!

-2
source

Source: https://habr.com/ru/post/1300238/


All Articles