1. Web Worker
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.
1.1. Web worker demo
// demo_workers.js
var i = 0;
function timedCount() {
i = i + 1;
postMessage(i);
setTimeout(timedCount, 500);
}
timedCount();
<!DOCTYPE html>
<html>
<body>
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
}
}
function stopWorker() {
w.terminate();
w = undefined; // can reuse the Web Worker when calling startWorker() again
}
</script>
</body>
</html>
1.2. reference
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers