Skip to content

Instantly share code, notes, and snippets.

@redbluish
Created November 17, 2013 02:04
Show Gist options
  • Select an option

  • Save redbluish/7508168 to your computer and use it in GitHub Desktop.

Select an option

Save redbluish/7508168 to your computer and use it in GitHub Desktop.
schedule.js
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