Friday, 3 July 2015

HTML5 Web Workers Tutorial

Web Worker give a straight forward means that for website to run scripts in background threads. Once created, a worker will send messages to the spawning task by posting messages to an occurrence handler given by the creator. In simple words, a web worker is a JavaScript running in the background, without affecting the performance of the page.
A html5 web worker tutorial may be a JavaScript that runs within the background,severally of alternative scripts,while not poignant the performance of the page. you’ll be able to still do no matter you want: clicking, choosing things, etc., the web worker runs within the background. All the browser support web worker.

Example:

<html>
<body>
<p> Numbers of mobile: <output id=”result”></output></p>
<button onclick=”startcounting()”>Start Counting</button>
<button onclick=”stopcounting()”>Stop Counting</button>
<br><br>
<script>
var x;
function startcounting()
{
if(typeof(counting)!==”undefined”)
{
if(typeof(x)==”undefined”)
{
x=new mobile(“example_mobile.js”);
}
w.onmessage = function (event) {
document.getElementById(“result”).innerHTML=event.data;
};
}
function stopcounting()
{
x.terminate();
}
</script>
</body>
</html>

Output:

Numbers of mobile:



Stop Web Worker:

Web Workers do not stop itself but the page that started them can stop them by calling terminate() method.
counting.terminate();

A terminated  html tutorial will no longer respond to messages or perform any additional computations. You cannot restart a worker; instead, you can create a new worker using the same URL.

No comments:

Post a Comment