正在載入此比較的其餘部分。

Untitled diff

建立於 差異永不過期
11 刪除
688 行
61 新增
724 行
1 Introduction
10.1 Introduction


1.1 Scope
10.1.1 Scope


This section is non-normative.
This section is non-normative.


This specification defines an API for running scripts in the background independently of any user interface scripts.
This specification defines an API for running scripts in the background independently of any user interface scripts.


This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.
This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.


Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.


Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.


1.2 Examples
10.1.2 Examples


This section is non-normative.
This section is non-normative.


There are a variety of uses that workers can be put to. The following subsections show various examples of this use.
There are a variety of uses that workers can be put to. The following subsections show various examples of this use.


1.2.1 A background number-crunching worker
10.1.2.1 A background number-crunching worker


This section is non-normative.
This section is non-normative.


The simplest use of workers is for performing a computationally expensive task without interrupting the user interface.
The simplest use of workers is for performing a computationally expensive task without interrupting the user interface.


In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number.
In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number.


The main page is as follows:
The main page is as follows:


<!DOCTYPE HTML>
<!DOCTYPE HTML>
<html>
<html>
<head>
<head>
<title>Worker example: One-core computation</title>
<title>Worker example: One-core computation</title>
</head>
</head>
<body>
<body>
<p>The highest prime number discovered so far is: <output id="result"></output></p>
<p>The highest prime number discovered so far is: <output id="result"></output></p>
<script>
<script>
var worker = new Worker('worker.js');
var worker = new Worker('worker.js');
worker.onmessage = function (event) {
worker.onmessage = function (event) {
document.getElementById('result').textContent = event.data;
document.getElementById('result').textContent = event.data;
};
};
</script>
</script>
</body>
</body>
</html>
</html>
The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker.
The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker.


The worker itself is as follows:
The worker itself is as follows:


var n = 1;
var n = 1;
search: while (true) {
search: while (true) {
n += 1;
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
if (n % i == 0)
continue search;
continue search;
// found a prime!
// found a prime!
postMessage(n);
postMessage(n);
}
}
The bulk of this code is simply an unoptimized search for a prime number. The postMessage() method is used to send a message back to the page when a prime is found.
The bulk of this code is simply an unoptimised search for a prime number. The postMessage() method is used to send a message back to the page when a prime is found.


View this example online.
View this example online.


1.2.2 Worker used for background I/O
10.1.2.2 Worker used for background I/O


This section is non-normative.
This section is non-normative.


In this example, the main document uses two workers, one for fetching stock updates at regular intervals, and one for performing search queries that the user requests.
In this example, the main document uses two workers, one for fetching stock updates at regular intervals, and one for performing search queries that the user requests.


The main page is as follows:
The main page is as follows:


<!DOCTYPE HTML>
<!DOCTYPE HTML>
<html>
<html>
<head>
<head>
<title>Worker example: Stock ticker</title>
<title>Worker example: Stock ticker</title>
<script>
<script>
// TICKER
// TICKER
var symbol = 'GOOG'; // default symbol to watch
var symbol = 'GOOG'; // default symbol to watch
var ticker = new Worker('ticker.js');
var ticker = new Worker('ticker.js');


// SEARCHER
// SEARCHER
var searcher = new Worker('searcher.js');
var searcher = new Worker('searcher.js');
function search(query) {
function search(query) {
searcher.postMessage(query);
searcher.postMessage(query);
}
}


// SYMBOL SELECTION UI
// SYMBOL SELECTION UI
function select(newSymbol) {
function select(newSymbol) {
symbol = newSymbol;
symbol = newSymbol;
ticker.postMessage(symbol);
ticker.postMessage(symbol);
}
}
</script>
</script>
<meta http-equiv="Refresh" content="120; URL=../">
<meta http-equiv="Refresh" content="120; URL=../">
</head>
</head>
<body onload="search('')">
<body onload="search('')">
<p><output id="symbol"></output> <output id="value"></output></p>
<p><output id="symbol"></output> <output id="value"></output></p>
<script>
<script>
ticker.onmessage = function (event) {
ticker.onmessage = function (event) {
var data = event.data.split(' ');
var data = event.data.split(' ');
document.getElementById('symbol').textContent = data[0];
document.getElementById('symbol').textContent = data[0];
document.getElementById('value').textContent = data[1];
document.getElementById('value').textContent = data[1];