在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ vue 函數(shù)節(jié)流中匿名函數(shù)問題

vue 函數(shù)節(jié)流中匿名函數(shù)問題

問題
這是原生版函數(shù)節(jié)流方法:匿名函數(shù)

(注: return function(){}methods中不能執(zhí)行)

請(qǐng)問, 怎么將其改成vue版呢?

由于在滾動(dòng)和中文搜索框中十分常用,請(qǐng)教

貼代碼:

created(){
    window.addEventListener('scroll', () => {
        this.debounce()(this._log, 1000);
    }
},
methods: {
    debounce (fn, idle) {
      let setTm;
      console.log('debounce')
      if (!idle || idle <= 0) return fn;
      return function () {
        clearTimeout(setTm);
        setTm = setTimeout(fn.bind(this, ...arguments), idle);
      }
    },
    _log(){
        console.log('_log')
    },
    
}
回答
編輯回答
負(fù)我心

綁定的方式應(yīng)該是:

 window.addEventListener('scroll', 
       this.debounce(this._log, 1000));

看下fiddle

2017年6月25日 07:05
編輯回答
不歸路
var throttle = (fun, wait = 5000) => {
    var last = 0;
    return function () {
        var args = arguments;
        var ctx = this;
        var now = Date.now();
        if (now - last >= wait) {
            last = now;
            return fun.apply(ctx, args);
        }
        console.log(`上一個(gè)周期未執(zhí)行完畢`)
    }
}

export default {
    name: 'VueComponent',
    methods: {
        scroll: throttle(function () {
            //...
        })
    }
}
2017年5月23日 20:19