How to stop the 1st ajax request if it is called again before the first call gets a response?
There might be some cases that your request takes long time because of some problems with your client internet connection or your s开发者_如何学JAVAerver connection. So since the client doesn't want to wait he clicks on the Ajax link again which sends the request to the server again which messes up the following:
- Rendering of our website in the browser because we are giving extra load to the browser.
- What if the second request processed correctly and you showed user the page and then comes along the error message from your first request(saying request timed out) which loads above on the correct content and mess up with the user reading the correct content.
I want to stop the 1st Ajax response if the Ajax function is called twice. How do I do this?
so i want to stop the 1st Ajax response if the Ajax function is called twice
What you actually want is to prevent a second request when a first request is in progress.
For example, You may have to change the Save
button to Saving...
, disable it (and add a little progress wheel) to give live feedback to the user. (Facebook does this)
The key is love feedback to the user. If the user is clueless on what is going on, they are going to think nothing is happening.
You might want to check why the operation is taking long
- If this is a complex/time consuming operation, like, say a report generation or a file upload, a progress bar should do
- If this is because of the client's internet connection, say it up front, like Gmail: Your have a slow Internet connection and this site may be slow. Better still, provide a fallback option, with less/no Ajax.
- You say cause we are giving extra load to the browser: this is kind of fishy. You will not be giving extra load to the browser unless you are giving it tons of HTML to render. Use Ajax only for small updates on the browser. You may want to reload the page if you expect a large change.
How bout seeing as your using some form of JavaScript to begin with you have that link either hidden or disabled in a manor of speaking til the pages request has been followed through with. You could for example have the requested ajax wait for a second variable that would enable that link so a user could click it. But until that variable is received from the original AJAX request its disabled to the click, then if it is clicked it disables again and waits for the same variable to come back again.
Think of how some developers disable a submit button on a form so a user can't double submit there form.. same concept of notion here.
精彩评论