定义节流函数,我这里是在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),