Last active
April 6, 2018 15:47
-
-
Save Tombarr/936a91be50645d51be240d205d29ac98 to your computer and use it in GitHub Desktop.
Track Global Timeouts 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
(() => { | |
let timeouts = []; | |
let intervals = []; | |
const _setTimeout = window.setTimeout; | |
const _setInterval = window.setInterval; | |
const _clearTimeout = window.clearTimeout; | |
const _clearInterval = window.clearInterval; | |
window.setTimeout = (func, delay) => { | |
let obj = Object.create(null); | |
obj[_setTimeout(func, delay)] = delay; | |
timeouts.push( obj ); | |
}; | |
window.setInterval = (func, delay) => { | |
let obj = Object.create(null); | |
obj[_setInterval(func, delay)] = delay; | |
intervals.push( obj ); | |
}; | |
window.clearTimeout = (id) => { | |
timeouts.splice(timeouts.findIndex((obj) => Object.keys(obj)[0] === id), 1); | |
_clearTimeout(id); | |
}; | |
window.clearInterval = (id) => { | |
intervals.splice(intervals.findIndex((obj) => Object.keys(obj)[0] === id), 1); | |
_clearInterval(id); | |
}; | |
window.getTimeouts = () => timeouts.splice(0); | |
window.getIntervals = () => intervals.splice(0); | |
window.getTimeoutCount = () => timeouts.length; | |
window.getIntervalCount = () => intervals.length; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment