How to prevent a double-click using jQuery?

前端 未结 15 824
孤街浪徒
孤街浪徒 2020-11-30 23:42

I have a button as such:


Within jQuery I am using the following, but it still

15条回答
  •  生来不讨喜
    2020-12-01 00:29

    This is my first ever post & I'm very inexperienced so please go easy on me, but I feel I've got a valid contribution that may be helpful to someone...

    Sometimes you need a very big time window between repeat clicks (eg a mailto link where it takes a couple of secs for the email app to open and you don't want it re-triggered), yet you don't want to slow the user down elsewhere. My solution is to use class names for the links depending on event type, while retaining double-click functionality elsewhere...

    var controlspeed = 0;
    
    $(document).on('click','a',function (event) {
        eventtype = this.className;
        controlspeed ++;
        if (eventtype == "eg-class01") {
            speedlimit = 3000;
        } else if (eventtype == "eg-class02") { 
            speedlimit = 500; 
        } else { 
            speedlimit = 0; 
        } 
        setTimeout(function() {
            controlspeed = 0;
        },speedlimit);
        if (controlspeed > 1) {
            event.preventDefault();
            return;
        } else {
    
            (usual onclick code goes here)
    
        }
    });
    

提交回复
热议问题