h5-vue-uniapp-节流防抖共通方案

描述:防止C端客户重复点击付款按钮而导致两次调起后台付款API

建议使用:


/**
* 防抖的函数
* @param {*} func 要执行的函数
* @param {*} await  等待时间
* @param {*} immidate   是否立即执行, 默认falseß
*/
function debounce(func, await, immidate = false){ 
   let timer =  null
   let debounce;
   debounce = function() {
       //this指向问题
       let context = this;
       //拿到参数
       let args = [...arguments];
       clearTimeout(timer);
       if(immidate){
           let callNow = !timer;
           timer = setTimeout(()=>{
               timer = null;
           }, await)

           //立即执行
           callNow && func.apply(context, args);
       } else {
           timer = setTimeout(()=>{
               func.apply(context, args);
           }, await)
       }
   }

   //取消防抖函数
   debounce.cancal = ()=>{
       clearTimeout(timer);
       timer = null;
   }
   return debounce
}



posted @ 2023-06-26 15:53  姜佳泉  阅读(81)  评论(0编辑  收藏  举报