Skip to content

Instantly share code, notes, and snippets.

@cramforce
Created June 24, 2011 13:48
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 cramforce/1044797 to your computer and use it in GitHub Desktop.
Save cramforce/1044797 to your computer and use it in GitHub Desktop.
function set() {
var origTimeout = window.setTimeout;
window.setTimeout = function(cb, time) {
if (time == null || time == 0 && typeof cb === 'function') {
var active = true;
var mc = new MessageChannel();
mc.port1.onmessage = function() {
if (active) {
cb();
}
};
mc.port2.postMessage(0);
var clear = function clearMessage() {
active = false;
};
clear.emulateClearTimeout = true;
return clear;
} else {
return origTimeout.apply(this, arguments);
}
};
var origClearTimeout = window.clearTimeout
window.clearTimeout = function(index) {
if (typeof index === 'function' && index.emulateClearTimeout) {
return index();
}
return origClearTimeout(index);
};
}
var script = '(' + set + ')()';
var s = document.createElement('script');
s.textContent = script;
document.getElementsByTagName('*')[0].insertBefore(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment