Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create a Web Worker file by HTML

Shulou Source: shulou.com Published: 2022-06-01 12:34:20 10月29日 Update

This article mainly shows you "HTML how to create a Web Worker file", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "HTML how to create a Web Worker file" this article.

The simplest use of Web staff is to perform time-consuming tasks. So here we will create a simple JavaScript task whose count ranges from zero to 100000.

Let's create an external JavaScript file named "worker.js" and type the following code.

Example download

Var I = 0

Function countNumbers () {

If (I < 100000) {

I = I + 1

PostMessage (I)

}

/ / Wait for sometime before running this script again

SetTimeout ("countNumbers ()")

}

CountNumbers ()

Note: Web worker cannot access DOM. This means that you cannot access any DOM elements in JavaScript code that you intend to run using Web Worker.

Tip: the postMessage () method of the worker object is used to send messages (such as the numbers in the example above) from the Web worker file back to the web page.

The above is all the content of the article "how HTML creates a Web Worker file". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files tasks content articles code learning help people elements such as above objects staff meaning numbers methods understandability more organization messages uses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux vpn Microsoft Docker