HTML5 API --- advanced usage of Web Worker

Source: Internet
Author: User

Web Worker is an important API introduced by HTML5. It enables JavaScript that has been criticized for running in a single thread to have multithreading and Web Worker, developers can put some heavyweight and time-consuming computing tasks in another thread to run, so that multiple threads can run at the same time to maximize the functionality of multiple CPU cores. The basic usage of Web Worker is as follows: var worker = new Worker ("background. js "); // background. js files are prepared in advance, and the JavaScript files in them are run in the background, which is generally heavyweight or time-consuming. So what if developers need to dynamically execute a series of JavaScript programs? Next we will introduce the advanced usage of Web Worker. How to dynamically specify the JavaScript program to run in Worker without creating a JS file in advance? We can create a Blob Object, assign the JavaScript text to the Blob Object, and then use window. URL. the creatObjectURL method generates a URL and creates a Worker object based on the URL. The JavaScript text just now runs in the Worker thread. In this way, the task is dynamically allocated to the Worker thread for execution, without the need to create a JavaScript file in advance. The instance code is as follows:

 


Related Article

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.