HTML5 Web Workers

Source: Internet
Author: User
Tags button type event listener

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.