What's the difference between Sync and Async in the method open(method,url,async) in AJAX.
Sync is synchronous, a.k.a. blocking; Async is asynchronous, a.k.a. non-blocking.
- When calling
open()
withasync=false
the request will take place before the call tosend()
returns. - When calling
open()
withasync=true
the call tosend()
will return immediately.
Setting async to false gives more predictable results as no other javascript code will run during the request but it has the significant downside of making the page unresponsive until the request is completed.
sync means that your javascript will be blocked until you get a response from the server
async means that the call to the server will happen in a parallel thread and your js will keep executing.
You should never use sync -- bad user interface to the human
Added: And you most probably shouldn't directly use XMLHttpRequest -- there are subtle differences between the browsers. Much better to use a library such as JQuery, yui, mootools, etc
If you pass true
for the async parameter, the call will return right away and the next line of javascript will execute (before the HTTP request returns).
If you pass false, it will block until the HTTP call returns.
The difference is that Sync doesn't exist. Ignore it. I wrote a whole synchronous widget library five years ago, and I became a better programmer when I rewrote it :-) I mention jQuery in this sentence but my preference goes to YUI. Whatever you do, watch the videos on Yahoo Theater: basic and advanced Javascript, the trouble with Dom, everything. They rock.