异步javascript加载/执行 [英] asynchronous javascript loading/executing

查看:85
本文介绍了异步javascript加载/执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这篇文章中,异步.js文件加载语法,有人说,如果存在async属性,那么只要脚本可用,脚本就会异步执行。

In this post, asynchronous .js file loading syntax, someone said, "If the async attribute is present, then the script will be executed asynchronously, as soon as it is available."

(function() {
  var d=document,
  h=d.getElementsByTagName('head')[0],
  s=d.createElement('script');
  s.type='text/javascript';
  s.async=true;
  s.src='/js/myfile.js';
  h.appendChild(s);
}()); /* note ending parenthesis and curly brace */

我的问题是,脚本将是什么异步执行是什么意思?这个脚本是否会在与页面中其他javascripts不同的线程中执行?如果是,我们是否应该担心两个线程中的同步问题?

My question is, what does "the script will be executed asynchronously" mean? Will this script be executed in a different thread from other javascripts in the page? If yes, should we worry about synchronization issue in the two threads?

谢谢。

推荐答案

它不会在不同的线程上执行。在这种情况下,您不必担心线程同步。

It will not be executed on different thread. You do not have to worry about thread synchronization in this case.

稍后,在您当前的调用堆栈展开后,myfile.js的下载将完成。浏览器和js框架将在此之后的某个时刻执行您的脚本。

At some point later, after your current call stack unwinds, the download of myfile.js will complete. The browser and js framework will execute your script at some point after that.

这篇关于异步javascript加载/执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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