在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时
utils.js
function debounce(fn, delay = 1000) {
let timer = null;
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments);
}, delay)
}
}
apply
arguments
arguments 是一个对应于传递给函数的参数的类数组对象。arguments
使用案例
import utils from "@/common/js/utils.js"
<input type="text" confirm-type="search" class="infoSearch-text" @input="getInputHandle()"
:placeholder="placeholder" placeholder-style="color:#999" style="color: #fff;" />
getInputHandle: utils.debounce(function(e) {
let value = e.detail.value;
this.$emit('input', value)
}),