Created
November 17, 2013 02:04
-
-
Save redbluish/7508168 to your computer and use it in GitHub Desktop.
schedule.js
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
| var schedule = (function (self) { | |
| var paused = false, // 标记状态 | |
| queue = []; // 队列 | |
| // 入队 | |
| self.join = function (fn, params) { | |
| params = params || {}; | |
| var args = [].concat(params.args); | |
| queue.push(function (_) { | |
| _.pause(); | |
| setTimeout(function () { | |
| fn.apply(params.context || null, args); | |
| _.resume(); | |
| }, params.delay || 1); | |
| }); | |
| return exec(); | |
| }; | |
| self.pause = function () { | |
| paused = true; // 忙碌 | |
| return this; | |
| }; | |
| // ready and call next | |
| self.resume = function () { | |
| paused = false; // 空闲 | |
| setTimeout(exec, 1); | |
| return this; | |
| }; | |
| function exec() { | |
| if (!paused && queue.length) { | |
| queue.shift()(self); // 出队 | |
| if (!paused) self.resume(); | |
| } | |
| return self; | |
| } | |
| return self; | |
| }(schedule || {})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment