Created
December 8, 2011 10:55
-
-
Save lukemorton/1446707 to your computer and use it in GitHub Desktop.
Syncronous (safe) setInterval()
This file contains hidden or 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
// Syncronous setInterval | |
// Usage: | |
// var interval = new SyncInterval(function () { | |
// console.log('bob'); | |
// }, 500); | |
// interval.stop(); | |
// Written by Luke Morton, Richard Willis | |
function SyncInterval(fn, wait, scope) { | |
var t = this; | |
wait = wait || 1000; | |
t.timeout = setTimeout(function () { | |
fn.call(scope); | |
t.timeout = setTimeout(arguments.callee, wait); | |
}, wait); | |
} | |
SyncInterval.prototype.stop = function () { | |
clearTimeout(this.timeout); | |
}; |
Aye very cool :)
Hey @drpheltright, it might be worth reverting the arguments.callee call, and instead naming the function. (It appears arguments.callee is deprecated and won't work in strict mode, see http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/)
t.timeout = setTimeout(function timer() {
fn.call(scope);
t.timeout = setTimeout(timer, wait);
}, wait);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hot shit, just noticed you used
arguments.callee
! That's awesome didn't know about that! Thanks @badsyntax