简体   繁体   English

异步执行的Javascript顺序

[英]Javascript order of asynchronous execution

I know there are lots of similar questions, but I am looking for feedback on a specific scenario. 我知道有很多类似的问题,但是我正在寻找有关特定情况的反馈。

Let's say I have a page that contains the following code: 假设我有一个包含以下代码的页面:

<script>
  var myCallback = function() {
      // stuff...
  }
</script>

<script async src="https://example.com/file.js?callback=myCallback"></script>

<script>
  var obj = {
    render: function() {
      this.funcA();
      this.funcB();
      this.funcC();
    },

    funcA: function() {
      // stuff
    },

    funcB: function() {
      // stuff
    },

    funcC: function() {
      // stuff
    }
  }

  obj.render();
</script>

The external script is set up to call 'myCallback' after it is loaded. 外部脚本被设置为在加载后调用“ myCallback”。

Is it possible that myCallback can be called while obj.render() is executing, or will it always wait until after render and all of its' subfunctions are called? 难道myCallback可以调用,而obj.render()正在执行,还是会一直等到render和它的所有子功能被称为?

In other words, is it possible for myCallback to be called sometime in between the execution of funcA , funcB , and funcC or will it always wait until after they are all executed? 换句话说,有可能是myCallback到某个时候的执行之间被称为funcAfuncB ,并funcC还是会一直等到他们都执行?

you cannot really tell when the asynch is completed. 您无法真正分辨出何时完成。 Most probably after obj.render() because it takes a while to initiate the asynchrone process. 很有可能在obj.render()之后,因为它需要一段时间才能启动异步过程。 In the meanwhile the code for obj.render is processed. 同时处理obj.render的代码。 But depending on such a code flow seems like tricky business. 但是依靠这样的代码流似乎是一件棘手的事情。 Let's hypothetical assume that the asynch code is loaded instantly because it is cached - then it might run before or while other parts of your code run. 假设我们假设异步代码是由于被缓存而立即加载的,那么它可能在代码其他部分运行之前或同时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM