views:

129

answers:

5

What's the difference between Sync and Async in the method open(method,url,async) in AJAX.

+1  A: 

Sync is synchronous, a.k.a. blocking; Async is asynchronous, a.k.a. non-blocking.

Hank Gay
A: 
  • When calling open() with async=false the request will take place before the call to send() returns.
  • When calling open() with async=true the call to send() 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.

Alexandre Jasmin
A: 

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

Larry K
-42 because you didn't mention jQuery at all. You know the rules.
Matt Dawdy
Oops, I meant jquery but I typed jscript. Fixed now. Sorry for the oversight. :-)
Larry K
+2  A: 

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.

pkaeding
+3  A: 

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.

Marco Mariani