【发布时间】:2019-04-26 12:20:41
【问题描述】:
我在 keyUp 上调用 searchkeyword 函数。我想在快速输入新字母时取消/clearTimeout $emit,以便只调用几次 $emit。 但是控制台在每次搜索关键字调用时都会被调用/去抖动。
methods: {
searchKeyword : function() {
var scope = this;
(this.debounce(function(){
scope.$emit("search-keyword", scope.keyword);
console.log("Called");
},350))();
},
debounce: function (func, delay) {
var timeout;
return function() {
const context = this;
const args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), delay);
}
}
}
【问题讨论】:
-
您必须只调用一次
debounce,而不是每次调用searchKeyword。
标签: javascript vue.js ecmascript-6 debounce