Skip to content

main #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2023
Merged

main #43

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions JavaScript/Advance/Web API/web-worker API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>web-worker API</title>
</head>

<body>
<h1>Web Worker API</h1>
<!-- Documentation -->
<button onclick="DisplayOne()">View Doc</button>
<p id="textOne"></p>
<!-- Example One -->
<h2>Count Example</h2>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<h1 id="textTwo"></h1>
<script src="script.js"></script>
</body>

</html>
29 changes: 29 additions & 0 deletions JavaScript/Advance/Web API/web-worker API/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Web Worker API
let textOne = document.getElementById('textOne');

function DisplayOne() {
textOne.innerHTML = `
<h2>Web Worker is a JavaScript running in the Background</h2>
<h3 style="font-family: sans-serif">When executing scripts in an HTML page, the page becomes unresponsive until the script is finished.

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>
`
}

// Example to Understand

let w;

function startWorker() {
if (typeof(w) == "undefined") {
w = new Worker("worker.js");
}
w.onmessage = function(event) {
document.getElementById("textTwo").innerHTML = event.data;
};
}

function stopWorker() {
w.terminate();
w = undefined;
}
9 changes: 9 additions & 0 deletions JavaScript/Advance/Web API/web-worker API/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let i = 0;

function timedCount() {
i++;
postMessage(i);
setTimeout("timedCount()", 500);
}

timedCount();