javascript: Clear all timeouts?

marcio picture marcio · Jan 14, 2012 · Viewed 73.8k times · Source

Is there a way to clear all time outs from a given window? I suppose the timeouts are stored somewhere in the window object but couldn't confirm that.

Any cross browser solution is welcome.

Answer

user123444555621 picture user123444555621 · Jan 14, 2012

They are not in the window object, but they have ids, which (afaik) are consecutive integers.

So you may clear all timeouts like so:

var id = window.setTimeout(function() {}, 0);

while (id--) {
    window.clearTimeout(id); // will do nothing if no timeout with id is present
}