Skip to content

Instantly share code, notes, and snippets.

@A1rPun
Last active August 17, 2017 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A1rPun/a82064e1a7f2ffc44164 to your computer and use it in GitHub Desktop.
Save A1rPun/a82064e1a7f2ffc44164 to your computer and use it in GitHub Desktop.
setTimeout setInterval timer handler. Access them via `window.timers`
(function(w){
var oldST = w.setTimeout;
var oldSI = w.setInterval;
var oldCI = w.clearInterval;
var timers = [];
w.timers = timers;
w.setTimeout = function(fn, delay){
var id = oldST(function(){
fn && fn();
removeTimer(id);
}, delay);
timers.push(id);
return id;
};
w.setInterval = function(){
var id = oldSI.apply(this, arguments);
timers.push(id);
return id;
};
w.clearInterval = function(id){
oldCI(id);
removeTimer(id);
};
w.clearTimeout = w.clearInterval;
function removeTimer(id){
var index= timers.indexOf(id);
if(index >= 0)
timers.splice(index, 1);
}
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment