Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1x 1x 2x 2x 1x 2x 2x 1x 1x 1x | /**
* 包装为延迟触发的函数
* - 用于处理密集事件,延迟时间内同时触发的函数调用。
* - 最终只在最后一次调用延迟后,执行一次。
* @method fn/delay
* @param {Function} fn 要延迟触发的函数
* @param {Number} duration 延迟时间(ms)
* @param {Object} [bind] 函数的this指向
* @returns {Function} 经过包装的延迟触发函数
* @example
* var $delay = require('@spore-ui/kit/packages/fn/delay');
* var comp = {
* countWords : function(){
* console.info(this.length);
* }
* };
*
* // 疯狂点击 input ,停下来 500 ms 后,触发函数调用
* $('#input').keydown($delay(function(){
* this.length = $('#input').val().length;
* this.countWords();
* }, 500, comp));
*/
function delay(fn, duration, bind) {
var timer = null;
return function () {
bind = bind || this;
if (timer) {
clearTimeout(timer);
}
var args = arguments;
timer = setTimeout(function () {
Eif (typeof fn === 'function') {
fn.apply(bind, args);
}
}, duration);
};
}
module.exports = delay;
|