How to prevent a double-click using jQuery?

前端 未结 15 849
孤街浪徒
孤街浪徒 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:28

    The solution provided by @Kichrum almost worked for me. I did have to add e.stopImmediatePropagation() also to prevent the default action. Here is my code:

    $('a, button').on('click', function (e) {
        var $link = $(e.target);
        if (!$link.data('lockedAt')) {
            $link.data('lockedAt', +new Date());
        } else if (+new Date() - $link.data('lockedAt') > 500) {
            $link.data('lockedAt', +new Date());
        } else {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();
        }
    });
    

提交回复
热议问题