HTML5 Web Workers / PDF
Sample of the pdf document :
What is a Web Worker?
When executing scripts in an HTML page, the page
becomes unresponsive until the script is finished.
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.
Browser Support
Web workers are supported in all major browsers,
except Internet Explorer.
HTML5 Web Workers Example
The example below creates a simple web worker that
count numbers in the background:
Example
Count numbers:
Start
Worker Stop Worker
Check Web Worker Support
Before creating a web worker, check whether the user's
browser supports it:
if(typeof(Worker)!=="undefined")
{
// Yes! Web worker support!
// Some code.....
}
else
{
// Sorry! No Web Worker support..
}
{
// Yes! Web worker support!
// Some code.....
}
else
{
// Sorry! No Web Worker support..
}
Create a Web Worker File
Now, let's create our web worker in an external
JavaScript.
Here, we create a script that counts. The script is
stored in the "demo_workers.js" file:
var i=0;
function timedCount()
{
i=i+1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();
The important part of the code above is the postMessage() method - which is used to posts a
message back to the H0....function timedCount()
{
i=i+1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();
Click here for Download PDF / FREE
0 commentaires: