Reputation: 2043
I am using ajax call in my classic asp application to execute a stored procedure.
But I don't want to wait until the stored procedure is running (Stored procedure taking approx 5-10 minutes to complete.).
The Ajax have to invoke the stored procedure and need to come back immediately.
I want Ajax call should not wait for response.
Here is my code snippet:
1) $.ajax({
type: "POST",
url: "runstoredprocedure.asp",
});
2) setInterval(function(){ jQuery("#list").trigger("reloadGrid"); },10000);
These are the two ajax calls I am using. the first one is running approxmately 5-7 min. Second one is not firing until first one completes. But immediately i need to call the second ajax call.
Can anyone help me on this issue.
Upvotes: 3
Views: 13669
Reputation: 15579
javascript fires off the request as part of a different thread and any code following your ajax call will be immediately executed. Having said that one misconception about JS async :
People take for granted that because it’s asynchronous, it’s a thread. They are partially right. There must be a thread created by the browser to keep the javascript running while it makes a request to the server. It’s internal and you don’t have access to that thread. But, the callback function called when the server responds to the ajax request is not in a thread.
I’ll explain clearer. If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called). That’s because javascript itself doesn’t create a thread to execute the ajax response from the server and simply waits that all executions are terminated before starting a new one.
So if you’re running a lot of ajax requests simultaneously, you might get some weird behavior because they will all wait one on another before executing themselves.
The last statement is relevant to your cause.
Excerpt from the blog: http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/
Interesting read: http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/
Upvotes: 5
Reputation: 17590
AJAX is by default asynchronous ( and it's the default option in all the javascript libraries ). For example, in jQuery:
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
You have a success, which takes a callback. When your action will finish, the callback will be called. jQuery will return immediately.
Upvotes: 2