Web worker is a powerful concept in javascript, which is used to improving performance of applications and it is running as Asynchronously.
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.
Usages for real-time:
If you are doing the complex operation in DOM, sometimes you feel like browser freezing or non functional.
Such case you can use Web workers
Coding:
<!DOCTYPE html>
<html>
<body>
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<p><strong>Note:</strong> Internet Explorer 9 and earlier versions do not support Web Workers.</p>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
// you can add operational code in demo_workers.js
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
}
}
function stopWorker() {
w.terminate();
w = undefined;
}
</script>
</body>
</html>
Note: sending and receiving data in the form of string only.
Once you get string you can parse do the opetations
Comments
Post a Comment