Loading

uniapp防止多次点击多次触发事件

1、common.js

//防抖节流
let timeout, result;
const debounce = function (func) {
    let args = arguments;
    console.log(args);
    if (timeout) {
        clearTimeout(timeout)
    }
    let callNow = !timeout
    timeout = setTimeout(() => {
        timeout = null;
    }, 1000)
    if (callNow) {
        // result = func.apply(this, args) //如this指向有问题再开启 并注释下一行
        result = func(...args)
    }
    return result
}

export default {
      debounce 
}

2、main.js

import global from '@/common/js/global.js'
Vue.prototype.$debounce = global.debounce // 防止多次点击事件

3、使用

<button class="cu-btn bg-blue shadow-blur round" @tap="$debounce(searchData,searchText)">搜索</button>

 

posted @ 2021-09-29 09:42  菜鸟的道路  阅读(2734)  评论(0)    收藏  举报