views:

291

answers:

4

I use the setTimeout() function through my application but when its time to garbage collect. the method still runs and calls on a function. How do I stop it from calling on a certain function. I tried setting it to null but it doesnt work

A: 

Nevermind, clearInterval()!

numerical25
`clearInterval()` is for killing a `setInterval()`, not a `setTimeout()`.
Jonathan Sampson
+1  A: 

See: clearTimeout()

AlishahNovin
+4  A: 

setTimeout returns an reference to the timeout, which you can then use when you call clearTimeout.

var myTimeout = setTimeout(...);
clearTimeout(myTimeout);
Matt Huggins
well, im a lil confused because clearInterval worked.
numerical25
Timeouts only occur once after X seconds, while intervals are used to repeat a timeout every X seconds. I guess clearInterval behaves the same as clearTimeout in whatever browser you're testing, but I'd make sure it works in all browsers if you're trying to use clearInterval on a timeout, and not on an interval.
Matt Huggins
A: 

Ditto. Use "clearInterval(timeoutInstance)".

If you are using AS3, I'd 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();
}
JONYC