Skip to content

Instantly share code, notes, and snippets.

@janily
Forked from sofish/shift.js
Created August 6, 2013 02:10
Show Gist options
  • Save janily/6161417 to your computer and use it in GitHub Desktop.
Save janily/6161417 to your computer and use it in GitHub Desktop.
// 顺延函数:如果上一个动作完成,则当前动作替换上一个
function shift(fn, time) {
time = time || 50;
var queue = this._shift_fn, current;
queue ? queue.concat([fn, time]) : (queue = [[fn, time]]);
current = queue.pop();
clearTimeout(this._shift_timeout);
this._shift_timeout = setTimeout(function() {
current[0]();
}, current[1]);
}
// 测试函数
function fn1() {
console.log('fn1');
}
function fn2() {
console.log('fn2');
}
function fn3() {
console.log('fn3');
}
// 最终因为 fn1 在 fn2 执行的时候还没完成,fn1 不执行,fn2 执行,fn3 在 fn2 执行后再执行,同样获得结果
shift(fn1, 10);
shift(fn2, 10);
setTimeout(function() {
shift(fn3);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment