Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created March 19, 2011 21:00
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 DavidBruant/877797 to your computer and use it in GitHub Desktop.
Save DavidBruant/877797 to your computer and use it in GitHub Desktop.
Wrapped setTimeout+clearTimeout
(function(global){
var setTimeout = global.setTimeout;
var clearTimeout = global.clearTimeout;
var timeoutIdentityObjects = [];
global.setTimeout = function(f, t){
var timeoutIdentityObject = new Object; // Insisting on the object being new and different from all previously generated
var i = setTimeout(function(){
f.call(); // Incomplete, because doesn't take strings and optinal arguments into consideration
delete timeoutIdentityObjects[i]; // For garbage collection
}, t);
timeoutIdentityObjects[i] = timeoutIdentityObject; // Keeping track of timeout identity to be able to delete it
return timeoutIdentityObject;
};
global.clearTimeout = function(o){
var i = timeoutIdentityObjects.indexOf(o); // With the hope that the native setTimeout always return a positive number...
if(i !== -1)
clearTimeout(i);
delete timeoutIdentityObjects[i]; // For garbage collection
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment