Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
See more:
How to use asynchronous versions of scripts to increase the page loading speed
Posted

1 solution

Web Worker in HTML 5
A web worker is a JavaScript running in the background, without affecting the performance of the page.
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.
Ex
<html>
<body>

<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button> 
<button onclick="stopWorker()">Stop Worker</button>
<br><br>

<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();
    w = undefined;
}
</script>

</body>
</html></br></br>
 
Share this answer
 
Comments
Sharon 2 23-Sep-14 6:10am    
Actually i'm not using html5.I have heard about the attribute async to synchronise scripts.But this will also work only in html5.Any other solutions please share.
[no name] 23-Sep-14 6:28am    
no wait for others answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900