Delaying click event

后端 未结 4 1149
滥情空心
滥情空心 2021-02-20 11:54

I\'m wondering whether there\'s a simple way to delay the click event from being processed for a specified period of time. For example we could have

$(\'#someEle         


        
4条回答
  •  后悔当初
    2021-02-20 12:45

    So in the end I figured out a way to solve the problem I posed, and I'm providing it here in case anyone else has the same problem and is looking for a solution.

    var secondClick = false;
    var duration = 1000;
    
    $('#someElement').on('click', 'a', function(event) {
        var that = $(this);
    
        if(!secondClick) {
            event.stopPropagation();
            setTimeout(function(){
                secondClick = true;
                that.click();
            }, duration);
    
            someAsynchronousFunction();
        } else {
            secondClick = false;
        }
    }
    

    Basically when the user clicks the link, it internally prevents that click from actually having any effect, and gives the asynchronous function a set amount of time to do it's work before doing a second click on the link which behaves normally.

提交回复
热议问题