Introduction to Web Worker

Source: Internet
Author: User

Introduction to Web Worker

In general, the browser will block the execution of a program until it returns to a normal state after the end of the run, and the HTML5 web worker is to solve the problem. Intensive calculations through worker threads to avoid program blocking and page lag (fps too low )

Example

Simulate the test with the Fibonacci sequence.

Worker-test.html

<! DOCTYPE html>

Worker.js

function fibonacci(n) { return n < 2 ? n : arguments.callee(n-1) + arguments.callee(n-2);}self.addEventListener(‘message‘, function (event) { let result = fibonacci(event.data); self.postMessage(result);});

Assume that the main page needs to perform time-consuming operations (such as: Fibonacci) multiple times, which can be performed asynchronously with workers as follows.

<!DOCTYPE html>

Introduction to Web Worker

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.