页面渲染可以与 JavaScript 同时运行吗?
我有一个重写的 GWT 对话框,它作为小部件重写了 PagingScrollTable。 我的代码中有这样的内容:
1) DialogBox dialog = new ...
2) dialog.center();
3) Window.alert("Hello");
在 IE 中,我会在加载表标题后但在加载表内容(大约 1000 行)之前看到警报。但是javascript是单线程语言怎么可能呢? 可能是浏览器问题还是我的代码问题?
谢谢!
I have an overridden GWT DialogBox that has as a widget overridden PagingScrollTable.
I have something like this in my code:
1) DialogBox dialog = new ...
2) dialog.center();
3) Window.alert("Hello");
In IE I would see alert after loading of table’s header but before loading of table’s content (about 1000 rows). But javascript is single-threaded language so how can it be?
May it be a browser issue or issue of my code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Javascript 可能是单线程的,但浏览器不是,并且页面渲染不是由 Javascript 引擎完成的,因此在页面渲染时 Javascript 可以运行并不存在冲突。
(事实上,您可以在
标记上添加
async
属性来明确告诉浏览器执行此操作,但遗憾的是,该属性在所有浏览器)Javascript may be single-threaded, but the browser isn't, and page rendering is not done by the Javascript engine, so there's no conflict that Javascript can be running while the page is rendering.
(in fact, you can add the
async
attribute on the<script>
tag to tell the browser explicitly to do this, although sadly this attribute isn't fully supported in all browsers yet)JavaScript 是异步的(它在浏览器的线程内运行,因此它不会像您想象的那样与页面加载同步。一旦 JavaScript 本身被加载,它只需要由事件触发即可运行,或者,它已加载的事实足以运行它。 '' 标签内容会立即由浏览器处理 - 因此,如果您的代码未包含在绑定到“卸载”或类似内容的“函数”中 -它会在任何时候运行它显示在 DOM 中。
Javascript is asynchronous (and it runs inside a thread in the browser so it's not in lock-step with the page loading the way you might think it is. Once the javascript itself is loaded, it need only be triggered by an event to run, or, the fact that it's loaded is enough to run it. '' tag contents are processed by the browser instantly - so if you have code that isn't wrapped in a 'function' that you bind to 'unload' or something similar - it'll just run whenever it shows up in the DOM.