Skip to content

Commit 398b376

Browse files
main
1 parent 6961c9d commit 398b376

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>web-worker API</title>
9+
</head>
10+
11+
<body>
12+
<h1>Web Worker API</h1>
13+
<!-- Documentation -->
14+
<button onclick="DisplayOne()">View Doc</button>
15+
<p id="textOne"></p>
16+
<!-- Example One -->
17+
<h2>Count Example</h2>
18+
<button onclick="startWorker()">Start Worker</button>
19+
<button onclick="stopWorker()">Stop Worker</button>
20+
<h1 id="textTwo"></h1>
21+
<script src="script.js"></script>
22+
</body>
23+
24+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Web Worker API
2+
let textOne = document.getElementById('textOne');
3+
4+
function DisplayOne() {
5+
textOne.innerHTML = `
6+
<h2>Web Worker is a JavaScript running in the Background</h2>
7+
<h3 style="font-family: sans-serif">When executing scripts in an HTML page, the page becomes unresponsive until the script is finished.
8+
9+
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.</h3>
10+
`
11+
}
12+
13+
// Example to Understand
14+
15+
let w;
16+
17+
function startWorker() {
18+
if (typeof(w) == "undefined") {
19+
w = new Worker("worker.js");
20+
}
21+
w.onmessage = function(event) {
22+
document.getElementById("textTwo").innerHTML = event.data;
23+
};
24+
}
25+
26+
function stopWorker() {
27+
w.terminate();
28+
w = undefined;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let i = 0;
2+
3+
function timedCount() {
4+
i++;
5+
postMessage(i);
6+
setTimeout("timedCount()", 500);
7+
}
8+
9+
timedCount();

0 commit comments

Comments
 (0)