The following Javascript code works in all browsers except IE9. What's the fix?
setTimeout('doSomething();clearTimeout();',500);
The debugger says that it's expecting me to pass an ID number to clearTimeout(), when all I'm doing is clearing all timeouts.
The following Javascript code works in all browsers except IE9. What's the fix?
setTimeout('doSomething();clearTimeout();',500);
The debugger says that it's expecting me to pass an ID number to clearTimeout(), when all I'm doing is clearing all timeouts.
Share Improve this question edited Sep 16, 2012 at 16:24 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Sep 14, 2012 at 21:28 VolomikeVolomike 24.9k22 gold badges126 silver badges217 bronze badges 9- 5 It's bad practice to pass a string to setTimeout. Pass a function instead. – asawyer Commented Sep 14, 2012 at 21:30
- 5 Well, there's really not much point in trying to clear a timeout that's already timed out. – Jonathan Lonowski Commented Sep 14, 2012 at 21:32
- 1 Why do you need clearTimeout there at all? Once it is already running there is no sense for clearTimeout – Viktor S. Commented Sep 14, 2012 at 21:33
-
3
I'd push all timeouts to an array then clear them in a
for
loop in that case. – Fabrício Matté Commented Sep 14, 2012 at 21:37 -
4
clearTimeout()
with no argument does not clear all timeouts. It just does nothing in all browsers; MDN documents it as "Passing an invalid ID to clearTimeout does not have any effect (and doesn't throw an exception", and running without argument is the same as running withundefined
argument, which is an invalid timeout ID. Test case:setTimeout(function() { console.log('timeout') }, 1000); clearTimeout();
Chrome still logs the 'timeout' message. – lanzz Commented Sep 14, 2012 at 21:39
1 Answer
Reset to default 6As others have said, clearTimeout()
without any arguments doesn't do anything. clearTimeout()
requires you to specify which timeout you want to clear. This applies in all browsers. Maybe only IE is reporting the error, but it's causing a problem in all of them.
Perhaps the reason you're calling clearTimeout()
is that you think the setTimeout()
needs to be cleared in case it keeps getting called? This is incorrect -- setTimeout()
causes its code to be called just once, after the given amount of time.
Therefore, since you're trying to clear the timeout after it's already been triggered, there's nothing to clear anyway.
There is a separate mand called setInterval()
which does trigger a repeating timeout. This would need to be cleared to stop it calling over and over and over, but it's generally considered to be a bad idea to use setInterval()
anyway, so I won't dwell on it too much here.
The other thing you should do is avoid calling setInterval
with a string parameter. Just specify the function you want to call directly, instead.
In short, your code should look like this:
setTimeout(doSomething,500);
That should be sufficient for your example to work fine in all browsers.
As I said, you don't need to clear the timeout in your case, but in case you do need to clear it in another instance, you would need to have setTimeout
(or setInterval
) return a value. This value is a reference object to the timer, and this reference is what you need to pass into clearTimeout
.
ie:
var timer;
....
timer = setTimeout(doSomething,500);
....
....
//something happened, I need to cancel the timeout:
clearTimeout(timer);
Hope that helps clear things up.