Prevent click event in jQuery triggering multiple times

后端 未结 6 602
甜味超标
甜味超标 2020-12-10 15:01

I have created a jQuery content switcher. Generally, it works fine, but there is one problem with it. If you click the links on the side multiple times, multiple pieces of c

6条回答
  •  情歌与酒
    2020-12-10 15:23

    One way to do this to use timeStamp property of event like this to gap some time between multiple clicks:

    var a = $("a"),
        stopClick = 0;
    
    
    a.on("click", function(e) {
      if(e.timeStamp - stopClick > 300) { // give 300ms gap between clicks
         // logic here
    
        stopClick = e.timeStamp; // new timestamp given
      }
    
    
    });
    

提交回复
热议问题