Created
March 19, 2011 21:00
-
-
Save DavidBruant/877797 to your computer and use it in GitHub Desktop.
Wrapped setTimeout+clearTimeout
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(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