What is the difference between Java MultiThreading, Linux Processes, and HTML5 Webmasters? What is a practical example of web workers and their use?

I am new to the HTML5 Webworker API. A web worker is JavaScript running in the background , without affecting page performance .

The following is a simple example for the Webworker API. If I started Web Worker and do not stop working with other pages and sites. In a few minutes, my browser will have more workload and problems with working with browsers. It works in the background, but if we do not stop Webworker, they will increase and affect performance, so how can I say that "without affecting the performance of the page .

Can someone give the right example or sugestion on the website for actual use and can we compare the web artist with the Java and Linux multithreading 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();
+4
source share
1 answer

Web workers do not interrupt UI flow

- javascript . javascript . , . javascript, , , , , , , , . (setTimeout ), webworkers, .

, , , , , -, , .

-

- , , , - , , API , .

:

  • DOM

, DOM , , . , , .

, Java, C concurrency , . , , . , , , concurrency.

?

, , .

:

+1

All Articles