事件稀释 throttle 与 debounce

throttle 函数:

window.addEventListener("resize", throttle(callback, 300, {leading:false}));
  window.addEventListener("resize", callback2);
  function callback ()  { console.count("Throttled"); }
  function callback2 () { console.count("Not Throttled"); }
 /**
  * 频率控制函数, fn执行次数不超过 1 次/delay
  * @param fn{Function}     传入的函数
  * @param delay{Number}    时间间隔
  * @param options{Object}  如果想忽略开始边界上的调用则传入 {leading:false},
  *                         如果想忽略结束边界上的调用则传入 {trailing:false},
  * @returns {Function}     返回调用函数
  */
 function throttle(fn,delay,options) {
     var wait=false;
     if (!options) options = {};
     return function(){
         var that = this,args=arguments;
         if(!wait){
             if (!(options.leading === false)){
                 fn.apply(that,args);
             }
             wait=true;
             setTimeout(function () {
                 if (!(options.trailing === false)){
                     fn.apply(that,args);
                 }
                 wait=false;
             },delay);
         }
     }
 }

  

debounce 函数:

window.addEventListener("resize", throttle(callback, 300, {leading:false}));
window.addEventListener("resize", callback2);
function callback ()  { console.count("Throttled"); }
function callback2 () { console.count("Not Throttled"); }
/**
 * 空闲控制函数, fn仅执行一次
 * @param fn{Function}     传入的函数
 * @param delay{Number}    时间间隔
 * @param options{Object}  如果想忽略开始边界上的调用则传入 {leading:false},
 *                         如果想忽略结束边界上的调用则传入 {trailing:false},
 * @returns {Function}     返回调用函数
 */
function debounce(fn, delay, options) {
    var timeoutId;
    if (!options) options = {};
    var leadingExc = false;

    return function() {
        var that = this,
            args = arguments;
        if (!leadingExc&&!(options.leading === false)) {
            fn.apply(that, args);
        }
        leadingExc=true;
        if (timeoutId) {
            clearTimeout(timeoutId);
        }
        timeoutId = setTimeout(function() {
            if (!(options.trailing === false)) {
                fn.apply(that, args);
            }
            leadingExc=false;
        }, delay);
    }
}

  

原文:http://www.cnblogs.com/zztt/p/4098657.html

posted on 2014-11-18 10:36 风之优雅z 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/windofelegant/p/4105110.html

throttledebounce都是用于控制函数的执行频率的方法。 throttle的作用是在一段时间内只执行一次函数,比如一个按钮的点击事件,在用户多次点击时只执行一次,以免出现重复操作的情况。 debounce的作用是在一段时间内不再触发函数后再执行函数,比如用户在输入框中输入文字时,只有在停止输入一段时间后才会执行搜索操作,以免频繁触发搜索操作。 下面是throttledebounce的使用方法: Throttle: ```javascript function throttle(func, delay) { let prev = Date.now() - delay; return function() { const context = this; const args = arguments; const now = Date.now(); if (now - prev >= delay) { func.apply(context, args); prev = now; } } } ``` 使用方法: ```javascript function handleClick() { console.log('clicked'); } const throttledClick = throttle(handleClick, 1000); button.addEventListener('click', throttledClick); ``` Debounce: ```javascript function debounce(func, delay) { let timer; return function() { const context = this; const args = arguments; clearTimeout(timer); timer = setTimeout(function() { func.apply(context, args); }, delay); } } ``` 使用方法: ```javascript function handleInput() { console.log('searching'); } const debouncedInput = debounce(handleInput, 500); input.addEventListener('input', debouncedInput); ``` 以上代码是纯JavaScript实现的throttledebounce,当然也可以使用一些第三方库来实现,比如lodash的throttledebounce函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值