Last active
November 24, 2015 14:43
-
-
Save kuzzmi/9a963824ab818a68feb1 to your computer and use it in GitHub Desktop.
Hooking timeouts and intervals to get data about active timers and intervals
This file contains hidden or 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
window._setTimeout = window.setTimeout; | |
window._setInterval = window.setInterval; | |
window._clearTimeout = window.clearTimeout; | |
window._clearInterval = window.clearInterval; | |
window.activeTimers = []; | |
window.activeIntervals = []; | |
function set(what, container, fn, delay, callback) { | |
var timerID, message; | |
var fnStr = fn ? fn.toString() : undefined; | |
var functionToCall = window['_set' + what]; | |
timerID = functionToCall(function() { | |
fn(); | |
clearTimeout(timerID); | |
}, delay); | |
container.push(timerID); | |
message = what + ' added. \r\nID: ' + timerID + '\r\nFunction: ' + fnStr + '\r\nDelay: ' + delay + '\r\nTotal: ' + container.length; | |
console.log(message); | |
callback(); | |
return timerID; | |
} | |
function clear(what, container, id, callback) { | |
var index = container.indexOf(id); | |
if (index === -1) { | |
return; | |
} | |
container.splice(index, 1); | |
console.log(what + ' removed. \r\nID: ' + id + '\r\nTotal: ' + container.length); | |
callback(); | |
window['_clear' + what ](id); | |
} | |
window.setTimeout = function(fn, delay) { | |
return set('Timeout', window.activeTimers, fn, delay, printTimers); | |
}; | |
window.clearTimeout = function(timerID) { | |
return clear('Timeout', window.activeTimers, timerID, printTimers); | |
}; | |
window.setInterval = function(fn, delay) { | |
return set('Interval', window.activeIntervals, fn, delay, printIntervals); | |
}; | |
window.clearInterval = function(timerID) { | |
return clear('Interval', window.activeIntervals, timerID, printIntervals); | |
}; | |
// Functions for printing debug information | |
function printTimers() { | |
console.log(window.activeIntervals.length); | |
} | |
function printIntervals() { | |
console.log(window.activeIntervals.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment