Skip to content

Instantly share code, notes, and snippets.

@Bartunek
Created January 22, 2014 10:29
Show Gist options
  • Save Bartunek/8556564 to your computer and use it in GitHub Desktop.
Save Bartunek/8556564 to your computer and use it in GitHub Desktop.
Native timers extended for passage of 'this' object
/**
*
* Enable the passage of the 'this' object through the JavaScript timers
*
**/
var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeST__(vCallback instanceof Function ? function () {
vCallback.apply(oThis, aArgs);
} : vCallback, nDelay);
};
window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeSI__(vCallback instanceof Function ? function () {
vCallback.apply(oThis, aArgs);
} : vCallback, nDelay);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment