Search code examples
jqueryjsonreload

Reload location after sending a form with JSON


I have a contact form. What I want to do, is when the user filled in the form and it passed all the checks (see if the field not empty, if the email is in fact an email etc...), it should display a confirmation message with animation and after the animation is done - reload the page.

So I tried it like that:

$.getJSON("inc/API.php", {
    command: "send_contact_form",
    ...rest of the keys and values...
}, function () {
    clear_form();
    $("#form_confirmation").animate({
        opacity: 1
    }, 1000, function () {
        setTimeout($("#form_confirmation").animate({
            opacity: 0
        }, 3000), 6000, function () {
            location.reload()
        });
    });
});

Everything works, except for the reload. I tried it in different locations, but I still don't see a reload of the page. What am I doing wrong here?


Solution

  • Try:

    setTimeout(function(){
        $("#form_confirmation").animate({opacity:0}, 3000, function(){
            location.reload();
        });
    },6000); 
    

    You currently have the location.reload() function as a third setTimeout() argument which isn't correct. The code above will reload the page once the confirmation message has been faded out.