__ __ EDIT
To control the set of timeouts (and intervals) you can use the following snippet. This will clear any timeouts or intervals set anywhere in the code, although you need to set this snippet before setting any timeout or interval. Basically, before processing any javascript code or an external script that uses a timeout / interval.
JS:
;(function () { window.timeouts = {}, window.intervals = {}, window.osetTimeout = window.setTimeout, window.osetInterval = window.setInterval, window.oclearTimeout = window.clearTimeout, window.oclearInterval = window.clearInterval, window.setTimeout = function () { var args = _parseArgs('timeouts', arguments), timeout = window.osetTimeout.apply(this, args.args); window.timeouts[args.ns].push(timeout); return timeout; }, window.setInterval = function () { var args = _parseArgs('intervals', arguments), interval = window.osetInterval.apply(this, args.args); window.intervals[args.ns].push(interval); return interval; }, window.clearTimeout = function () { _removeTimer('timeouts', arguments); }, window.clearInterval = function () { _removeTimer('intervals', arguments); }, window.clearAllTimeout = function () { _clearAllTimer('timeouts', arguments[0]); }, window.clearAllInterval = function () { _clearAllTimer('intervals', arguments[0]); }; function _parseArgs(type, args) { var ns = typeof args[0] === "function" ? "no_ns" : args[0]; if (ns !== "no_ns")[].splice.call(args, 0, 1); if (!window[type][ns]) window[type][ns] = []; return { ns: ns, args: args }; } function _removeTimer(type, args) { var fnToCall = type === "timeouts" ? "oclearTimeout" : "oclearInterval", timerId = args[0]; window[fnToCall].apply(this, args); for (var k in window[type]) { for (var i = 0, z = window[type][k].length; i < z; i++) { if (window[type][k][i] === timerId) { window[type][k].splice(i, 1); if (!window[type][k].length) delete window[type][k]; return; } } } } function _clearAllTimer(type, ns) { var timersToClear = ns ? window[type][ns] : (function () { var timers = []; for (var k in window[type]) { timers = timers.concat(window[type][k]); } return timers; }()); for (var i = 0, z = timersToClear.length; i < z; i++) { _removeTimer(type, [timersToClear[i]]); } } }());
How to use it:
Set timeout (s) / interval (s) as usual:
var test1 = setTimeout(function(){, 1000); var test2 = setTimeout(function(){, 1000);
Then you can use to clean both:
clearAllTimeout(); // clearAllInterval(); for intervals
This will clear both timeouts ( test1 and test2 )
You can use some namespaces to remove only certain timers, for example:
// first (optional) parameter for setTimeout/setInterval is namespace var test1 = setTimeout('myNamespace', function(){/**/, 1000); // 'myNamespace' is current namespace used for test1 timeout var test2 = setTimeout(function(){/**/, 1000); // no namespace used for test2 timeout
Again, clearAllTimeout(); will clear both timeouts. To remove only one instance, you can use:
clearAllTimeout('myNamespace'); // clearAllInterval('myNamespace'); for namespaced intervals
This will clear only test1 timeout
For some reason, you can only remove intermediate timeouts without names. Then you can use:
clearAllTimeout('no_ns'); // clearAllInterval('no_ns'); for non namespaced intervals only
In this example, only test2 timeout will be cleared
__ END EDIT __
An old article specific to open the question here:
You can try the following:
var timeouts = []; timeouts.push(setTimeout(function() { social2(); }, 5000)); timeouts.push(setTimeout(function() { social1(); }, 5000));
UPDATED after comment by David Thomas
var timeouts = {'social' : [], 'antisocial' : []}; //a social timeout timeouts.social.push(setTimeout(function() { social1(); }, 5000)); //an anti-social timeout timeouts.antisocial.push(setTimeout(function() { antisocial1(); }, 5000)); function clearTimeouts(namespace){ for(var i = 0, z = timeouts[namespace].length; i < z; i++) clearTimeout(timeouts[namespace][i]); timeouts[namespace] = []; } //usage eg clearTimeouts("social");