如何使用脚本的异步版本 [英] How to use asynchronous versions of scripts
本文介绍了如何使用脚本的异步版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用异步版本的脚本来提高页面加载速度
How to use asynchronous versions of scripts to increase the page loading speed
推荐答案
HTML 5中的Web Worker
Web worker是一个在后台运行的JavaScript,不会影响页面的性能。
Web worker是一个在后台运行的JavaScript,独立于其他脚本,不影响页面的性能。您可以继续执行任何操作:在Web工作程序在后台运行时,单击,选择内容等。
Ex
Web Worker in HTML 5
A web worker is a JavaScript running in the background, without affecting the performance of the page.
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.
Ex
<html>
<body>
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<br><br>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
}
}
function stopWorker() {
w.terminate();
w = undefined;
}
</script>
</body>
</html></br></br>
这篇关于如何使用脚本的异步版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文