Skip to content

Instantly share code, notes, and snippets.

@Tombarr
Last active April 6, 2018 15:47
Show Gist options
  • Save Tombarr/936a91be50645d51be240d205d29ac98 to your computer and use it in GitHub Desktop.
Save Tombarr/936a91be50645d51be240d205d29ac98 to your computer and use it in GitHub Desktop.
Track Global Timeouts and Intervals
(() => {
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