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内联svg教程以及与canvas的区别

html5停止(暂停)当前播放的音频或视频的方法pause()

html5规定是否对元素进行拼写和语法检查的属性spellcheck

如何使用html5中postmessage实现ajax中的post跨域问题的详细介绍

h5的语义化标签

html5拖放关于api实现拖放排序的实例代码

怎么学html5?

分享一个html5实现拖放的实例代码

h5中header标签应该如何使用

html5中的nav标签的详解

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




打赏

取消

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

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

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

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

评论

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