6

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";
});
3
  • What you want execute while the client is waiting response? Commented Aug 25, 2012 at 20:52
  • Sorry, I don't know what I'm doing, it sounded best? I guess its not though? Commented Aug 25, 2012 at 21:15
  • And fwiw anything that you write in beforeSend is going to block the ajax request from firing until that code block has completed. So it actually doesn't do what you want at all. Commented Aug 25, 2012 at 21:23

3 Answers 3

15

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.

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

Comments

3

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
   }
   // ......
 });

1 Comment

same comment as the other answer: beforeSend is going to block the ajax request from firing. This does not accomplish what the question had asked of executing code in the browser while the ajax request was being processed on the server.
1

You can't execute a function while another is being executed in JS, being it mono-threaded: you can do stuff before and after though - are you looking for a way to setup a message / spinner to be shown while waiting? Check beforeSend option in $.ajax() call:

$.ajax({
    type: "POST",
    url: "mail.php",
    data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
    beforeSend : function(){

        // do your stuff here

    }
    }).done(function(){
        alert("Your message was sent. We will be in contact with you shortly.");
        window.location="index.html";
});

2 Comments

beforeSend is going to block the ajax request from firing. This does not accomplish what the question had asked of executing code in the browser while the ajax request was being processed on the server.
You're totally right, thank you for letting me have clue on proper beforeSend purpose.

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.