Last active
August 17, 2017 09:50
-
-
Save A1rPun/a82064e1a7f2ffc44164 to your computer and use it in GitHub Desktop.
setTimeout setInterval timer handler. Access them via `window.timers`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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