Created
February 12, 2014 06:01
-
-
Save r3b/8950719 to your computer and use it in GitHub Desktop.
Turn a constant, erratic method execution into a nice, steady pace.
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
| function defib(fn, space, scope) { | |
| space || (space = 1000); | |
| var queue = [], timer; | |
| function start() { | |
| if (queue.length) { | |
| var exec = queue.shift(), | |
| context = exec.context, | |
| args = exec.args; | |
| fn.apply(context, args); | |
| } else { | |
| console.error("queue empty; clear timer.") | |
| clearInterval(timer); | |
| } | |
| } | |
| return function() { | |
| var context = scope || this, | |
| args = [].concat(Array.prototype.slice.call(arguments)); | |
| queue.push({ | |
| context: context, | |
| args: arguments | |
| }); | |
| if (!timer) timer = setInterval(start, space); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment