As we know $.ajax()
Is a asynchronous method , beacuse next statement starts executing before ajax()
method is fully executed and 'ajax()' keep doing his stuff parallelly ,And hide()
is a Synchronous method, because it immediately hides the element and next statement will execute when hide()
really done his whole task, But I am really confused in the case of hide("slow")
. It seems Asynchronous but I read, it sets the timer in browser and everything happen automatically(now hide("slow")
is doing nothing parallelly) so in a way , It has also been done its whole task before the next statement start executing ,So hide("slow")
also seems a synchronous method ,
I am very confused about this Synchronous Asynchronous concept
Can someone help me to understand this concept ?
The call to the method is synchronous, but it starts an asynchronous process. So we would normally, loosely, call it an "asynchronous method" (in this case, where you're giving it a duration argument).
When you call
hide("slow")
, you synchronously tell jQuery to start the process of of hiding the element slowly over time. The process of actually doing that takes place asynchronously, after the initial call tohide
is complete. (This is also true ofajax
: The method itself is synchronous, but the process it starts — doing the XMLHttpRequest — continues asynchronously.)Typically if the work of the method is completed during the call to it, we call it a synchronous method, but if it only starts work that completes later, we call it an asynchronous method. Technically the method itself isn't asynchronous, just the overall process that it initiates, but...
hide
itself, of course, is both a synchronous and asynchronous method depending on what argument(s) you pass it: If you call it with no duration (.hide()
), it's synchronous; if you call it with a duration (.hide("slow")
,.hide(400)
), it's asynchronous.Also, for your another question,
When you debug a line of code and the line is not passing to the next line until the execution completes, its synchronous operation since the same thread is performing the complete execution of the code.
On the other hand, if debugging is passing to the next line before the actual operation completes then its asynchronous operation since as in your question above, the hide & animation are performed in the background with different thread while the main thread started executing the next line.
Hope it clarifies your question above.