I am new to HTML5 Webworker API. A web worker is a JavaScript running in the background, without affecting the performance of the page.
Given below is simple example for Webworker API. If I start the Webworker and don't stop working with other pages and sites. After few minutes my browser will has more load and problem with working with browsers. It is running in background but if we not stop the Webworker, they will increase and affecting the performance issue so, How can I state that "without affecting the performance of the page".
Can anyone give the proper example or sugestion on webwork for actual use and Can We compare the webworker with the Java Multithreading and Linux Process ?
demo.html
<!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, your browser does not support Web Workers...";
}
}
function stopWorker() {
w.terminate();
}
</script>
</body>
</html>
demo_workers.js
var i=0;
function timedCount() {
i=i+1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();