Prevent double submission of forms in jQuery

后端 未结 22 1596
旧时难觅i
旧时难觅i 2020-11-22 08:10

I have a form that takes a little while for the server to process. I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button agai

22条回答
  •  独厮守ぢ
    2020-11-22 08:30

    There is a possibility to improve Nathan Long's approach. You can replace the logic for detection of already submitted form with this one:

    var lastTime = $(this).data("lastSubmitTime");
    if (lastTime && typeof lastTime === "object") {
        var now = new Date();
        if ((now - lastTime) > 2000) // 2000ms
            return true;
        else
            return false;
    }
    $(this).data("lastSubmitTime", new Date());
    return true; // or do an ajax call or smth else
    

提交回复
热议问题