0
点赞
收藏
分享

微信扫一扫

js通过settimeout实现防抖功能(亲测有效)

妖妖妈 2022-03-30 阅读 45
防抖jses6
 function toast(text, time) {
        let toast = document.getElementById('toast');
       let toast_box = document.getElementsByClassName('toast_box')[0];
       toast.innerHTML = text;
       toast_box.style.animation = 'show 1.5s';
       toast_box.style.display = 'inline-block';
    setTimeout(function(){
        toast_box.style.animation = 'hide 1.5s';
     setTimeout(function(){
        toast_box.style.display = 'none';
    }, 1000)
    }, time)
   }
   function fn(callback){
        fn.prototype.init(callback);
    }
            
    fn.prototype = {
        canclick: true,
         init: function(callback){
      if(this.canclick){
        this.canclick = false
   callback();
   setTimeout(function(){
        this.canclick = true
    }.bind(this),2500)
      }else{
        console.log('1s中之内不允许重复点击');
    }
         }
            }
     $(".a").click(function(){
        console.log("a");
    fn(function(){
        toast("异常提醒", 1400);
    });
            })

这里用jquery中的$(".类名")来进行特定元素的绑定

举报

相关推荐

0 条评论