Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to detect whether the browser supports WebWorker

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report