Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts

    setTimeout on form submit?

    Hello, I'm having trouble getting the setTimeout to work with my form submission. I want the form to submit 3 seconds after I click the submit button (to allow a confirmation message to be faded in and out). Here is what I have curently, but the setTimeout seems to be getting ignored for some reason, and the form just submits right away as if there was no setTimeout on it at all (therefore the confirmation message is not allowed enough time to fade in and out entirely, before being taken to the target url that the submit button takes you to after submitting the form)-

    $('#formSubmitButton').click(function() {
    $('#formMessageSentConfirmation').fadeIn(1000).delay(1000).fadeOut(1000);
    setTimeout(function() { if (!validate(form)) return; }, 3000)
    setTimeout(function() { form.submit(); }, 3000)
    });

    It's the last two lines here that I want to set a delay of 3 seconds on. How can I do this?

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi
    Try replace form with: $('form') , because just simple form returns error if it is not an object, or reference to a form element.

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I replaced them as you said, and there is no difference, it is still submitting right away. Here's what I have now-

    $('#formSubmitButton').click(function() {
    $('#formMessageSentConfirmation').fadeIn(1000).delay(1000).fadeOut(1000);
    setTimeout(function() { if (!validate($('form'))) return; }, 3000)
    setTimeout(function() { $('form').submit(); }, 3000)
    });

    Do I need to change something else?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by resin View Post
    I replaced them as you said, and there is no difference, it is still submitting right away. Here's what I have now-

    $('#formSubmitButton').click(function() {
    $('#formMessageSentConfirmation').fadeIn(1000).delay(1000).fadeOut(1000);
    setTimeout(function() { if (!validate($('form'))) return; }, 3000)
    setTimeout(function() { $('form').submit(); }, 3000)
    });

    Do I need to change something else?
    Remove the quotes around 'form' which makes it a literal.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Using click() will not prevent the default submit action of the form.

    I would use submit(), return false from this (to prevent the form submission) and then (as in your current code) call .submit() in the time out to trigger the submission.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •