H5的多线程如何实现Web Worker


当前第2页 返回上一页

当线程发生错误的时候,它的onerror事件回调会被调用。

1

2

3

4

var worker = new Worker("test.js");

worker.onerror = function(event){

    console.log("load web worker error." + event);

}

发送JSON数据

复杂的数据就用JSON传送吧!

Web Worker中使用importScripts加载外部JS

在HTML页面中,使用 <script>

标签加载外部的JS文件,而<script>标签还支持 跨域 加载JS。

在Web Worker中要注意!

Worker实例化的时候必须传入一个脚本的URL,而这个URL必须是在本域下,否则会报跨域错误! var worker = new Worker('https://localhost/worker.js');

但可以在worker.js中通过importScripts方法引入任何域下的脚本,如同HTML中的<script>标签一样。下面是合法的使用方法:

1

2

3

4

importScripts(); /* imports nothing */ importScripts(‘foo.js’); /* imports just “foo.js” */ importScripts(‘foo.js’, ‘bar.js’); /* imports two scripts */ importScripts(‘//example.com/hello.js’); /* You can import scripts from other origins */

可以利用这里的importScripts方法解决资源预加载的问题(浏览器预先加载资源,而不会对资源进行解析和执行),道理也很简单。

Scripts may be downloaded in any order, but will be executed in the order in which you pass the filenames into importScripts() . This is done synchronously; importScripts() does not return until all the scripts have been loaded and executed.

</script>


相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

html5中的DOM编程的实现步骤

用h5做出微信的支付过程的实现步骤

用H5做有特效的下拉框

以上就是H5的多线程如何实现Web Worker的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

html5面试题pc端和移动端区别

html5新增标签--nav标签介绍

html5如何在手机端调用相机?

html5原生拖拽操作实例讲解

html5实现如何在两个div元素之间拖放图像

h5的标题书写问题

关于html中<label>标签的for属性的详细分析

html5中关于volume属性的使用详解

html5的音频和视频监听器的应用代码详解

html5初窥2之新元素

更多相关阅读请进入《Worker》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...