HTML5 Web Workers is a JavaScript that runs in the background and does not affect the performance of the page.
What is a web worker?
When you execute a script in an HTML page, the state of the page is not responsive until the script finishes running.
The web worker in HTML5 is a JavaScript running in the background, independent of other scripts, without affecting the performance of the page. You can continue to do any other events that you would like to do, such as: Click, select Content, etc.
The web worker is running in the background, so it does not affect the performance of the page.
Instance:
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; charset =utf-8 "/>
<title></title>
<meta charset=" Utf-8 "/>
<style type=" Text/css ">
</style>
<script type= "Text/javascript"
Function Startworker () {
var w;
if (typeof (Worker)!== "undefined") {//before creating a Web Worker, check to see if your browser supports it.
if (typeof (W) = = "undefined") {//check whether the Web Worker object exists, or create a new Web worker object if it does not exist
W = new Worker ("Webworker.js");
W.onmessage = function (event) {//Adds an event listener to the Web Worker object
document.getElementById ("result"). InnerHTML = Event.data;
};
}
else {
document.getElementById ("Result"). InnerHTML = "sorry!, your browser does not support Web worker in HTML5.";
}
}
function Stopworker () {
W.terminate ();//terminame () method terminates browser or computer resources
w = undefined;
}
</script>
<body>
<p> count: <output id= "Result" >0</output></p>
<button type= "button" onclick= "Startworker ()" > Click Start </button>
<button type= "button" onclick= "Stopworker ()" > Click Stop </button>
</body>
The Webworker.js file code is as follows:
var i = 0;
function Timedcount () {
i = i + 1;
PostMessage (i); the//postmessage () method is used to send a message back to the HTML page
SetTimeout ("Timedcount ()", 1000);
}
Timedcount ();
HTML5 Web Workers