Why ajax is asynchronous




















The synchronous option in AJAX is there for the small number of situations in which you can't use an asynchronous call but reloading the entire page is unnecessary. For example, you might need to handle some transaction processing in which the order is important.

Consider a case in which a web page needs to return a confirmation page after the user clicked something. This task requires synchronizing the requests. Actively scan device characteristics for identification. Use precise geolocation data. Select personalised content. Create a personalised content profile. Measure ad performance. Select basic ads. Create a personalised ads profile.

Select personalised ads. Apply market research to generate audience insights. Measure content performance. Develop and improve products. List of Partners vendors. Asked 10 years ago. Active 4 years, 9 months ago.

Viewed 19k times. Improve this question. Tiny Bhavesh Bhavesh 2 2 gold badges 3 3 silver badges 9 9 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. You didn't explain what was "not at the same time" about AJAX. Your answer makes it sound like you can work at the same time as the call, so therefore it is called asynchronous, which makes no sense grammatically.

Therefore, this answer makes sense. Yes it is confusing since the dictionary defines it as existing or occurring at the same time period. Asynchronous would therefore mean not happening in the same period -- but this is clearly not so with user tasks employing an XMLHttpRequest object.

Unlike what today said, you do need to look at how user requests are accepted with respect to previously submitted requests. From this standpoint, synchronous request acceptance demands that user request B must await completion of some prior request A.

Asynchronous allows sending requests with no regard to the status of prior requests. Jerry Coffin Jerry Coffin k 74 74 gold badges silver badges bronze badges. Thank you -- I finally understand what exactly is considered "synchronous" and "asynchronous"! It has always puzzled me that when you run two AJAX calls at the same time synchronized they are called "asynchronous". You explained it nicely. WindowsMaker WindowsMaker 2, 6 6 gold badges 26 26 silver badges 44 44 bronze badges.

Neither AJAX nor asynchrony are equal to simple repeated polling to prevent the user from having to refresh a page manually. With this, all codes following this one will execute immediately; you will not have to wait until the function returns. This way, your user would not have to wait for findItem to return a result. Of course, because you still need an answer, you would have to include a callback.

A callback is a function that would give you the result or response you wanted, and it is typically called after all statements after the findItem has been executed. The first solution has already been mentioned above. You can write asynchronous AJAX calls so that it waits for the response before moving on to the next statements. If you are using jQuery, you can easily do this by setting the async option to false. While this might help you solve the problem of getting a return response to your Ajax call, there is no doubt that you will run into problems using this solution.

For one, if there are any long processes in a JavaScript code, it will lock the user interface and make it unresponsive. JavaScript execution times are also limited, and when it reaches its limit, the browser will prompt the user to ask whether he or she wants to continue executing the code or not. Both the browser hanging and a pop-up window asking the user to continue to translate to a bad user experience.

This is especially true with users who have a slow Internet connection. A much better solution is to make your functions accept callbacks and rewrite your code around these. If you have:. This will make the foo accept a callback, which it will then use as success callback.

Then you can set up a function as an argument to foo, like this:. When this code executes, callback will go back to the original function that we have set up to foo. There are times when directly passing a callback is not ideal for your application. The good news is that for these situations, you can rely on deferred objects, or those that represent a value that may be available at a certain point in the future.

In short, using deferred objects, you can chain your Ajax calls without nesting any part of your code.



0コメント

  • 1000 / 1000