简体   繁体   English

外部托管的javascript文件的代码能否与Page中的window.load并行执行

[英]Can an externally hosted javascript file's code get executed parallel with window.load in a Page

What is the execution flow of the external hosted javascript files with respect to the window.onload invokation. 关于window.onload调用,外部托管的javascript文件的执行流程是什么。 Say we have below code in the external file: 假设外部文件中包含以下代码:

(function() {

   console.log('logging1...');
})();

var func1 = function() {
   console.log('func1 invoked!');
};

func1();

Before the window.load gets fired, 在触发window.load之前,

1.) What is the execution flow or output of the above code? 1.)以上代码的执行流程或输出是什么?

2.) Is there a possibility for any code of the file getting executed after window.load? 2.)window.load之后是否有可能执行文件的任何代码?

3.) Parallel with window.load, can an external file code be executed. 3.)与window.load并行,可以执行外部文件代码。

1.) What is the execution flow or output of the above code? 1.)以上代码的执行流程或输出是什么?

Usually it is: 通常是:

logging1...
func1 invoked!

There is no ambiguity in the order of function invocations here. 这里的函数调用顺序没有任何歧义。 But pay attention that console.log() is not standardized to immediately print the output. 但是请注意, console.log()尚未标准化,无法立即打印输出。 It could very well return synchronously without printing the given arguments in the order of console.log() function invocations. 如果不按console.log()函数调用的顺序打印给定的参数,它很可能可以同步返回。

2.) Is there a possibility for any code of the file getting executed after window.load? 2.)window.load之后是否有可能执行文件的任何代码?

No (apart from code in event listeners, timers and the like, of course). 否(当然,除了事件侦听器,计时器等中的代码之外)。

The load event fires at the end of the document loading process. 加载事件在文档加载过程结束时触发。 At this point, all of the objects in the document are in the DOM, and all the images, scripts , links and sub-frames have finished loading . 此时,文档中的所有对象都在DOM中,并且所有图像, 脚本 ,链接和子框架均已完成加载 ( https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload ) https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload

3.) Parallel with window.load, can an external file code be executed. 3.)与window.load并行,可以执行外部文件代码。

What do you mean with parallel? 你说并行是什么意思? JavaScript is single threaded. JavaScript是单线程的。

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

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