javascript 为什么 AJAX 被称为异步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7932690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is AJAX called asynchronous?
提问by Bhavesh
Why is AJAX called asynchronous? How does it accomplish communication asynchronously with the server?
为什么 AJAX 被称为异步?它是如何与服务器异步完成通信的?
回答by hvgotcodes
It's asynchronous in that it doesn't lock up the browser. If you fire an Ajax request, the user can still work while the request is waiting for a response. When the server returns the response, a callback runs to handle it.
它是异步的,因为它不会锁定浏览器。如果您触发 Ajax 请求,则在请求等待响应期间用户仍然可以工作。当服务器返回响应时,会运行一个回调来处理它。
You can make the XMLHttpRequest
synchronous if you want, and if you do, the browser locks up while the request is outstanding (so most of the time this is inappropriate)
您可以根据需要进行XMLHttpRequest
同步,如果您这样做,浏览器会在请求未完成时锁定(因此大多数情况下这是不合适的)
回答by Jerry Coffin
It's asynchronous because the client and the server run independently of each other for the duration of the function call.
它是异步的,因为客户端和服务器在函数调用期间彼此独立运行。
During a normal function call, you make the call, and the calling function doesn't get to execute again until the function call finishes and returns. The caller and the callee are always synchronized.
在正常的函数调用期间,您进行调用,并且在函数调用完成并返回之前,调用函数不会再次执行。调用者和被调用者总是同步的。
During an asynchronous function call, you make the call, and then control returns immediatelyto the caller. The callee then returns a value some indeterminate amount of time later. That "indeterminate amount of time" means the caller and callee are no longer synchronized, so it's asynchronous.
在异步函数调用期间,您进行调用,然后控制权立即返回给调用者。被调用者然后在某个不确定的时间后返回一个值。“不确定的时间量”意味着调用者和被调用者不再同步,所以它是异步的。
回答by WindowsMaker
Simply put, it does not need to reload the whole page to get new information. Think of a email client. You would not need to refresh the page to see new emails. Ajax just pulls the server every couple of minutes to see if there are new emails, if so display them
简单地说,它不需要重新加载整个页面来获取新信息。想想电子邮件客户端。您无需刷新页面即可查看新电子邮件。Ajax 只是每隔几分钟拉一次服务器,看看是否有新邮件,如果有则显示它们
回答by eclewlow
I.e. not "blocking", within the context of Javascript execution, as the response will be handled by an event loop.
即不是“阻塞”,在 Javascript 执行的上下文中,因为响应将由事件循环处理。
回答by G D Vki
The client and the server run independently of each other for the duration of the function call.
在函数调用期间,客户端和服务器彼此独立运行。
Normal function call - you make the call, and the calling function doesn't get to execute again until the function call finishes and returns. The caller and the callee are always synchronized.
正常函数调用 - 您进行调用,并且在函数调用完成并返回之前,调用函数不会再次执行。调用者和被调用者总是同步的。
Asynchronous function call - you make the call, and then control returns immediately to the caller. The callee then returns a value some undefined amount of time later. That "undefined amount of time" means the caller and callee are no longer synchronized, so it's asynchronous.
异步函数调用 - 您进行调用,然后控制立即返回给调用者。被调用者然后在一些未定义的时间后返回一个值。“未定义的时间量”意味着调用者和被调用者不再同步,因此它是异步的。
回答by Tofazzal Al Hoque
Synchronous always maintain sequence when called, but asynchronous is not maintain sequence.
调用时同步总是保持顺序,但异步不保持顺序。