In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use Worker in HTML5, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this HTML5 article on how to use Worker. Let's take a look at it.
One: how to use Worker
The basic principle of Web Worker is that in the current main thread of javascript, use the Worker class to load a javascript file to open up a new thread, which has the effect of not blocking execution each other, and provides an interface for data exchange between the main thread and the new thread: postMessage,onmessage.
So how to use it, let's look at an example:
/ / worker.jsonmessage = function (evt) {var d = evt.data;// get the sent data postMessage (d) through evt.data; / / send the acquired data to the main thread}
HTML page: test.html
/ / WEB page main thread var worker = new Worker ("worker.js"); / / create a Worker object and pass it the URL worker.postMessage ("hello world") of the script to be executed in the new thread; / / send data to worker worker.onmessage = function (evt) {/ / receive the data function console.log (evt.data) from worker; / / output the data from worker}
After opening test.html with a Chrome browser, the console output "hello world" indicates that the program was executed successfully.
From this example, we can see that using web worker is mainly divided into the following parts
WEB main thread:
1. Create a worker by loading a JS file with worker = new Worker (url) and return an instance of worker.
two。 Send data to worker through the worker.postMessage (data) method.
3. Bind the worker.onmessage method to receive the data sent by worker.
4. You can use worker.terminate () to terminate the execution of a worker.
New thread for worker:
1. Send data to the main thread through the postMessage (data) method.
two。 Bind the onmessage method to receive the data sent by the main thread.
Second: what can Worker do?
Know how to use web worker, so what is the use of it, can help us solve those problems. Let's look at an example of the fibonacci sequence.
As we all know, in mathematics, the fibonacci sequence is defined recursively as follows: F0 ∈ 0 fibonacci (n Mel 1) + F (n mi 2) (n > = 2 m m n N *). The common implementation of javascript is:
Var fibonacci = function (n) {return n
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.