1. // 防抖函数
    2. export function debounce(func, delay) {
    3. let timer //定义一个变量接收参数
    4. return (...args) => {
    5. if (timer) {
    6. clearTimeout(timer) //每次进入先清一次定时器,防止上次的定时器还在执行
    7. }
    8. timer = setTimeout(() => {
    9. func.apply(this, args) //更改this指向
    10. }, delay)
    11. }
    12. }
    1. // 联想搜索
    2. this.$watch('searchinput', debounce((newQuery) => {
    3. if(newQuery) {
    4. this.fnSearch(newQuery)
    5. }
    6. }, 500), { immediate: false })