underscore.js: _.throttle(function, wait)

前端 未结 5 1105
轮回少年
轮回少年 2021-02-19 13:21

According the underscore documentation:

throttle_.throttle(function, wait)
Creates and returns a new, throttled version of the pa

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 13:37

    To extend Darhazer's answer

    It's more like, except _.throttle is called imminently and then again after delay milliseconds

    function throttle(func, delay) {
        var timer = 0;
    
        return function() {
            var context = this,
                args = [].slice.call(arguments);
    
            clearTimeout(timer);
            timer = setTimeout(function() {
                func.apply(context, args);
            }, delay);
        };
    }
    

提交回复
热议问题