.hide(“slow”) is synchronous or Asynchronous metho

2019-03-06 19:55发布

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 ?

2条回答
相关推荐>>
2楼-- · 2019-03-06 20:26

.hide(“slow”) is synchronous or Asyncronous method

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 to hide is complete. (This is also true of ajax: 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.

查看更多
别忘想泡老子
3楼-- · 2019-03-06 20:28

Also, for your another question,

I am very confused about this Synchronous Asynchronous concept Can someone help me to understand this concept ?

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.

查看更多
登录 后发表回答