并传递给jQuery的$。获得的回调()在一个单独的线程中执行? [英] does the callback passed to jquery's $.get() execute in a separate thread?

查看:111
本文介绍了并传递给jQuery的$。获得的回调()在一个单独的线程中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题:假设我有以下的JS / jQuery的code

Very simple question: suppose I have the following js/jquery code

doSomething();

$.get(url, callback);

doSomethingElse();

据我所知,GET请求发送后立即doSomethingElse()开始执行。现在假设而doSomethingElse()所在的服务器的回复到达。发生了什么?

I understand that right after the GET request is sent doSomethingElse() starts being executed. Now suppose that the server's reply arrives while doSomethingElse() is executing. What happens?

  • 是否回调运行在独立的线程在平行于doSomethingElse()?
  • 是否doSomethingElse()暂停执行,直至回调运行和回报?
  • 是否回调仅被调用一次doSomethingElse()返回?

感谢您的任何见解!

拉​​拉

推荐答案

没有,JavaScript的Web浏览器是单线程的,由设计。这意味着,虽然AJAX调用可能会立即开始(由另一个线程在浏览器中处理),您的回调将不会发生,直到JavaScript的跨preTER是一个空闲的。可以做的事得到排队等待跨preTER成为闲置和处理它们。

No, JavaScript in web browsers is single-threaded, by design. That means that although the ajax call may start immediately (and be processed by another thread in the browser), your callback won't happen until the JavaScript interpreter is next idle. Things to do get queued up waiting for the interpreter to become idle and process them.

修改回答您的具体问题:

是否到doSomethingElse()?运行在独立的线程中并行回调

Does the callback run in a separate thread in parallel to doSomethingElse()?

没有,回调将在同一逻辑线程的 doSomethingElse 运行。 (这将是依赖于实现的是否是同一个实际的底层操作系统的线程,但你没有办法知道的和你不关心; 逻辑的,它是在同一个线程)

No, the callback will run in the same logical thread as doSomethingElse. (It would be implementation-dependant whether that's the same actual underlying OS thread, but you have no way of knowing and you don't care; logically, it's the same thread.)

请问doSomethingElse的执行()暂停,直至回调运行和回报?

Does the execution of doSomethingElse() pause until the callback runs and returns?

默认情况下, GET 将是异步的,所以没有。 doSomethingElse 发起的请求,但随后继续。 (这是可能做的同步的通过底层XmlHtt prequest机制得到的,但它是一个非常糟糕的主意 - 往往完全锁住浏览器的用户界面,而请求正在运行,这是丑陋的 - 我不知道你是怎么用jQuery做)

By default, the get will be asynchronous, so no. doSomethingElse initiates the request, but then continues. (It's possible to do a synchronous get via the underlying XmlHttpRequest mechanism, but it's a very bad idea -- tends to lock up the UI of the browser completely while the request is running, which is ugly -- and I don't know how you do it with jQuery.)

请问回调仅被调用一次doSomethingElse()返回?

Does the callback only get called once doSomethingElse() has returned?

使用异步 GET (通常的那种),你可以肯定的是, doSomethingElse 将回调前完成被调用,是的。这是因为JavaScript的跨preTER只会做一件事的时候,它不能切换到做一个新的东西,直到它与当前的完成。因此,尽管 doSomethingElse 触发GET(和GET可以通过其他非JavaScript线程并行的JavaScript处理),您的回调将不会发生,直到跨$后p $ PTER与 doSomethingElse 和任何调用它。

With an asynchronous get (the usual kind), you can be certain that doSomethingElse will finish before the callback gets called, yes. This is because the JavaScript interpreter will only do one thing at a time, and it doesn't switch to doing a new thing until it's done with the current one. So although doSomethingElse triggers the get (and the get may be processed by other, non-JavaScript threads in parallel to JavaScript), your callback won't happen until after the interpreter is done with doSomethingElse and anything that called it.

我也不会感到惊讶,如果在某些时候,我们开始越来越多线程,基于浏览器的JavaScript,但如果当我们这样做时,就必须是明确的,因为我们都高兴地假设一个线程的时刻。

I wouldn't be surprised if at some point we start getting multiple threads in browser-based JavaScript, but if and when we do, it'll have to be explicit, since we all happily assume one thread for the moment.

这篇关于并传递给jQuery的$。获得的回调()在一个单独的线程中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆