Created
August 5, 2013 13:34
-
-
Save sofish/6155950 to your computer and use it in GitHub Desktop.
顺延执行: 顺延函数:如果上一个动作完成,则当前动作替换上一个 (如自动完成插件的远程获取)
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 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
就是说fn2在解析的时候顺手把fn1的timeout给kill了,所以fn1就不执行了=。=
那么如果fn3的timeout的时间比fn2还短的话,前面两都不执行。