How can I execute a function that will run while the client is waiting for the server response? Here is my code. I looked up and found a .load() function, but how does that fit into this? Any help would be great! Thanks
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
}).done(function(){
alert("Your message was sent. We will be in contact with you shortly.");
window.location="index.html";
});
The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.
If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").
jQuery $.post() Method The $.post() method requests data from the server using an HTTP POST request. Syntax: $.post(URL,data,callback); The required URL parameter specifies the URL you wish to request.
The very next line of code you write after You call $.ajax() will run while the browser is waiting for a response.
So:
$.ajax();
yourAwesomeFN();
XhttpRequests are asynchronous. No extra work required.
Have you looked in the "beforeSend" param.
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()},
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With