pause jQuery function for 5 seconds before script returns true and posts
Im using jQuery to post a form to a database, before posting the HTML form to a remote server via the form action method.
All works fine, but I am trying to build in a message pre-the server POST , so is there a way that just before my jQuery function returns true to get it to pause for 5 seconds and display a message?
...
$.ajax({
type: "POST",
url: "myform.php",
data: dataString,
success: function() {
$('#details').html("<div id='post-message'></div>");
开发者_运维技巧 $('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch</h2>")
//.append("<p>Thank you</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='tick' src='images/tick.png' />");
});
}
});
return true;
});
...
I ideally need a pause of 5 seconds just before the script returns true and finishes - any help much appreciated! Presently the message has no time to display as the script returns true and posts!
Thanks
You could use setTimeout('func', 5000);
in the success function.
function doItLater()
{
$('#details').html("<div id='post-message'></div>");
$('#post-message').html("<h2 style='color:#fff'>Thanks for getting in touch</h2>")
//.append("<p>Thank you</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='tick' src='images/tick.png' />");
});
}
$.ajax({
type: "POST",
url: "myform.php",
data: dataString,
success: function() {
// do stuff now ...
// wait 5 seconds
window.setTimeout('doItLater', 5000);
return true;
});
});
If you need to be sure that POST request is complete before doing some other things, try setting "async" param to "false" - see http://api.jquery.com/jQuery.ajax/
精彩评论