Skip to content

Instantly share code, notes, and snippets.

@idettman
Created May 12, 2014 18:19
Show Gist options
  • Save idettman/4cea50a6cb936636aa59 to your computer and use it in GitHub Desktop.
Save idettman/4cea50a6cb936636aa59 to your computer and use it in GitHub Desktop.
JS - Allow scope binding for timer and setInterval
/* JavaScript polyfill to fix scope issues with the setInterval */
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