惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - 魔豆

oracle中使用unpivot实现列转行 推荐一个可以下载B站视频的工具 学习qt,做了一个小应用:随机点名提问系统 hadoop的eclipse插件 mysql5.5安装到最后一步卡死无响应的解决方法 Hbase各版本支持情况 Comparable接口的使用 css3实现动画效果 HTML5中使用EventSource实现服务器发送事件 css3中的盒子模型 使用visual studio code运行html 【转】解决chrome浏览器不支持audio和video标签的autoplay自动播放 spring mvc中添加对Thymeleaf的支持 Eclipse安装代码反编译插件Enhanced Class Decompiler 使用idea创建web项目 shell编程学习笔记(十二):Shell中的break/continue跳出循环 shell编程学习笔记(十一):Shell中的while/until循环 shell编程学习笔记(十):Shell中的for循环 shell编程学习笔记(九):Shell中的case条件判断
HTML5中的Web Worker技术
魔豆 · 2019-05-27 · via 博客园 - 魔豆

  为了让后台程序更好的执行,在HTML5中设计了Web Worker技术。Web Worker的产生主要是考虑到在HTML4中JavaScript Web程序都是以单线程的方式执行的,一旦前面的脚本花费时间过长,后面的程序就会因长期得不到响应而使用户页面操作出现异常。

  Web Worker实现的是线程技术,可以使运行在后台的JavaScript独立于其他脚本,从而不会影响页面的性能。

  示例:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>Document</title>
 8 </head>
 9 <body>
10     <div id="num"></div>
11     <button onclick="startWork()">开始</button>
12     <button onclick="stopWork()">停止</button>
13     <script>
14         var work;
15 
16         function startWork() {
17             if(typeof(Worker)!=undefined) {
18                 if(work==undefined) {
19                     work = new Worker("script/01.js");
20                     work.onmessage = function(event) {
21                         document.getElementById("num").innerHTML = event.data;
22                     }
23                 }
24             }
25         }
26 
27         function stopWork() {
28             if(work!=undefined) {
29                 work.terminate();
30             }
31         }
32     </script>
33 </body>
34 </html>

上面的代码引入了一个脚本文script/01.js,代码如下:

 1 var i=0;
 2 
 3 function timeCount() {
 4     i=i+1;
 5     console.log(i);
 6     postMessage(i);
 7     setTimeout("timeCount()",1000);
 8 }
 9 
10 timeCount();

这个示例实现了一个计数器的功能,点击开始按钮,开始计数,点击停止按钮,停止计数。

下面我来解释一下代码的流程:

网页第17行,检测浏览器是否支持Web Worker技术,主流的浏览器谷歌、火狐都支持,IE只有IE10,IE11,IE Edge支持,低版本的IE不支持

网页第19行,引入js脚本,创建一个Worker对象的实例,并执行js脚本

js脚本第6行,在执行脚本的时候,使用postMessage方法,传递给网页数据

网页第20行,在脚本使用postMessage方法传递数据后,onmessage回调函数会接收传递过来的数据,对该数据进行处理

网页第19行,如果需要停止Worker,使用work.terminate();可以停止正在执行的脚本