Submit button withdrawal

you can enable the send withdrawal of the form (like in gmail) and abort the submission. Add this code to functions.php

ref. https://wordpress.org/support/topic/withdrawal-button/

var delay = 2000; // set the preferred delay before send email
var interval = null; // don't touch this

jQuery(function ($) {
    // displays the button in active state
    function sendDisplayON(btn) {
        btn.val("Abort?");
        btn.addClass("submitting");
        $(btn).siblings(".ajax-loader").css("visibility", "visible");
    }

    // displays the button in normal state
    function sendDisplayOFF(btn) {
        btn.val("Send");
        btn.removeClass("submitting");
        $(btn).siblings(".ajax-loader").css("visibility", "hidden");
    }

    // on submit
    $('.wpcf7 input[type="submit"]').on("click", function (e) {
        e.preventDefault(); // prevent form submit

        var btn = $(this); // store this button into a variable

        if (btn.hasClass("submitting")) {
            clearInterval(interval); // reset the timeout
            sendDisplayOFF(btn);
        } else {
            sendDisplayON(btn);

            interval = setInterval(function () {
                // waits delay var value then will trigger this function

                clearInterval(interval); // reset the timeout
                sendDisplayOFF(btn);

                wpcf7.submit(btn.closest("form")[0]); // non ajax forms -> btn.closest("form")[0].submit;
            }, delay);
        }
    });
});

Post navigation

You might be interested in...

No comments yet, be the first!

Comments

Your email address will not be published. Required fields are marked *