...

View Full Version : setTimeout on form submit?



resin
07-24-2012, 11:19 AM
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?

MarPlo
07-24-2012, 04:01 PM
Hi
Try replace form with: $('form') , because just simple form returns error if it is not an object, or reference to a form element.

resin
07-25-2012, 06:39 AM
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?

Philip M
07-25-2012, 07:46 AM
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.

AndrewGSW
07-25-2012, 12:28 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum