throttle debounce 总结

2017-07-13  本文已影响576人  evelynlab

理解throttle debounce

throttle debounce 实现

以throttle-debounce 包代码解析:
debounce.js:

var throttle = require('./throttle');
module.exports = function ( delay, atBegin, callback ) {
    return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
};

throttle.js:

module.exports = function ( delay, noTrailing, callback, debounceMode ) {
    var timeoutID;
    // Keep track of the last time `callback` was executed.
    var lastExec = 0;
    if ( typeof noTrailing !== 'boolean' ) {
        debounceMode = callback;
        callback = noTrailing;
        noTrailing = undefined;
    }
    function wrapper () {
        var self = this;
        var elapsed = Number(new Date()) - lastExec;
        var args = arguments;
        // Execute `callback` and update the `lastExec` timestamp.
        function exec () {
            lastExec = Number(new Date());
            callback.apply(self, args);
        }
        function clear () {
            timeoutID = undefined;
        }
        if ( debounceMode && !timeoutID ) {
            exec();
        }
        if ( timeoutID ) {
            clearTimeout(timeoutID);
        }
        if ( debounceMode === undefined && elapsed > delay ) { 
            // throttle时,根据时间戳之差是否大于delay决定是否执行回调          
            exec();
        } else if ( noTrailing !== true ) {
            // debounce时,setTimeout延迟执行回调
            timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
        }
    }
    return wrapper;
};

应用
shop.js: // 输入关键字查询门店

import { throttle, debounce } from 'throttle-debounce';
...
debounce(200, async(val) => { await this.getShopList(val); });
...

还可以参考underscore, lodash 中throttle, debounce的实现,做了更多的兼容和考虑。

throttle debounce 应用场景

throttle:
监听resize事件做一些DOM元素的定位更改;
监听scroll 事件判断上拉时是否要加载数据
debounce:
搜索框输入发送ajax请求,监听onchange or keyup 事件进行查询;

Reference

1.https://github.com/lishengzxc/bblog/issues/7
2.http://www.jianshu.com/p/fb08b7ef31de

上一篇 下一篇

猜你喜欢

热点阅读