4

I've implemented this 'locksubmit' plugin http://blog.leenix.co.uk/2009/09/jquery-plugin-locksubmit-stop-submit.html where it changes the display state of the button to disabled. I then want the form to be delayed by a few seconds before posting to the form URL.

What would I have to add in or modify to delay the form "post" once the user has clicked the submit button?

Thanks!

2 Answers 2

22

Cancel the default form submit behaviour, and start a timeout at the same time:

$('form').submit(function (e) {
    var form = this;
    e.preventDefault();
    setTimeout(function () {
        form.submit();
    }, 1000); // in milliseconds
});

This should be compatible with the locksubmit plugin.

See a demo here: http://jsfiddle.net/7GJX6/

Sign up to request clarification or add additional context in comments.

3 Comments

And why does this not cause infinite recursion and no form submission?
It's not recursive because of e.preventDefault() and using this.submit() instead of $('form').submit(), which would cause an infinite loop.
What is the different between this and $(this) that prevents the recursion?
0

I think this is what you're looking for - modify lockSubmit() as such:

jQuery(':submit').lockSubmit(function(){
  setTimeout(5e3); // fancy 5 seconds
});

And with options object:

 jQuery(':submit').lockSubmit({
   submitText: "Please wait",
   onAddCSS: "submitButtons",
   onClickCSS: "submitButtonsClicked"
},function(){
  setTimeout(5e3);
}));

Basically javascript has to wait for all arguments to be resolved before executing the body. By setting timeout during the argument we delay the return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.