Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
258
9
棘手的问题,例如线程安全、竞争条件和锁,
Web
职程在单独的执行上下文中运行。
Web
职程不能与网页、浏览器窗口或其他代码交互。为了强调这一点,
Worker
对象
要求你把
Web
职程的代码放在单独的文件中,然后在创建职程时提供文件路径。
worker = new Worker('prime-worker.js');
理解这个限制之后,
Web
职程模型的其他内容就水到渠成了。应用与职程之间通过
消息传递通信。若想发送消息,调
postMessage()
。在本节的素数示例中,页面
发送一个有两个属性的对象字面量,即
to
from
,表示搜索的范围。
worker.postMessage({from: fromNumber, to: toNumber});
职程响应时,调用
postMessage()
发送素数数组。
postMessage(primes);
发送消息的频率没有限制。例如,创建职程之后,你可以调用
postMessage()
发送
一些工作,然后闲置一会,再调用
postMessage()
发送更多工作。
Web
职程也可以
使用
setTimeout()
setInterval()
函数安排定期工作。
停止职程有两种方法。第一种,调用
close()
方法停止职程。更常用的方法是,由
创建职程的页面调用
worker.terminate()
关闭职程。由这种方法停止的职程不能
再复活。
9.7.4
参考
完整的代码,包括搜索素数的例程,参见本书的示例代码(
https://github.com/
javascripteverywher ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov
JavaScript

JavaScript

David Flanagan
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9787519869601