定義節流函數,我這里是在Vue中使用的
function throttle(fn, delay) {
var lastTime;
var timer;
var delay = delay || 200;
return function () {
var args = arguments;
// 記錄當前函數觸發的時間
var nowTime = Date.now();
clearTimeout(timer);
timer = setTimeout(function () {
// 記錄上一次函數觸發的時間
lastTime = nowTime;
// 修正this指向問題
fn.apply(this, args);
}, delay)
}
};
在methos中方法中調用,我這里是阻止change多次觸發問題
checkChange: throttle(function (data, checked, childCked) {
var self = this;
var dictionaryViewVue = self.dictionaryViewVue;
dictionaryViewVue.modelIds = dictionaryViewVue.$refs.refTree.getCheckedKeys();
if (dictionaryViewVue.cardType == 'example') {
dictionaryViewVue.getEampleData();//解決重復發送請求
} else {
dictionaryViewVue.getIndexData();//解決重復發送請求
}
}, 50),