In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to detect whether the browser supports WebWorker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Check whether the browser supports WebWorker
Before you create a webworker, check if the user's browser supports it:
If (typeof (Worker)! = "undefined")
{
/ / Yes! Web worker support!
/ / some code.
}
Else
{
/ / Sorry! Web Worker does not support
}
Create a webworker file
Now, let's create our webworker in an external JavaScript.
Here, we create a count script. The script is stored in the "demo_workers.js" file:
Var iTunes 0
Function timedCount ()
{
I=i+1
PostMessage (I)
SetTimeout ("timedCount ()")
}
TimedCount ()
The important part of the above code is the postMessage () method, which is used to return a message to the HTML page.
Note: webworker is not usually used for such simple scripts, but for tasks that consume more CPU resources.
Create a WebWorker object
We already have the webworker file, and now we need to call it from the HTML page.
The following code detects whether worker exists, and if not,-it creates a new webworker object and then runs the code in "demo_workers.js":
If (typeof (w) = = "undefined")
{
W=new Worker ("demo_workers.js")
}
Then we can generate and receive messages from webworker.
Add a "onmessage" event listener to webworker:
W.onmessage=function (event) {
Document.getElementById ("result") [xss_clean] = event.data
}
Terminating WebWorker
When we create the webworker object, it continues to listen to the message (even after the external script completes) until it is terminated.
To terminate webworker and release browser / computer resources, use the terminate () method:
W.terminate ()
WebWorkers and DOM
Because webworker is in an external file, they cannot access the following JavaScript objects:
Window object
Document object
Parent object
This is the end of the content of "how to detect whether the browser supports WebWorker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.