JavaScript: Understanding the Worker Event API

Source: Internet
Author: User

If you are not familiar with event events, it is recommended to read my previous text javascript: Understanding DOM events. Or look directly below the worker API.

First, we need to instantiate a worker object, and the browser will open a new interface based on the newly created Worker object, which will handle the communication between the client and the INDEXEDDB database. The database here refers to the browser database. If you need to determine whether the browser supports worker objects, see the following code. or whether the browser supports the INDEXEDDB database, see the same, the two are best to choose the former. Because IE does not support INDEXEDDB.

if (window. Worker) {dosomething}
Worker
= WINDOW.INDEXEDDB | | WINDOW.MOZINDEXEDDB | | Window.webkitindexeddb
if (!WINDOW.INDEXEDDB) {dosomething}
IndexedDB

The worker object then sends data through the PostMessage thread to the INDEXEDDB database, and when the INDEXEDDB database receives the data sent by the client, it first stores and records the key values of the data into the INDEXEDDB database table. In fact, it is equivalent to storing the data in a fully structured table.

Therefore, the INDEXEDDB database will throw the received data values to the new interface processing, when the new interface to obtain data and parse, will be thrown back through the PostMessage data to the database, the database receives the return of the data processing in the same way as above, At this point the INDEXEDDB database throws the returned data to the OnMessage thread that the client accepts the parameters, and the main thread behind the OnMessage threads is the data that is sent back.

/*koringz.github.io** $author koringz* $data 2015-12-24* $version 0.1**/varTXT1 = Document.queryselector ("#txt1")varTXT2 = Document.queryselector ("#txt2")varTXT3 = Document.queryselector ("#txt3")varresult = Document.queryselector ("#result") Window.indexeddb= WINDOW.INDEXEDDB | | WINDOW.MOZINDEXEDDB | | WINDOW.WEBKITINDEXEDDB | |Window.msindexeddb; if(!window.indexeddb) {Console.log ("Your browser does not support INDEXEDDB"); }    if(window. Worker) {var_this =NewWorker (".. /.. /js/build/scroll_ten1.js "); Txt1.onchange=function() {_this.postmessage ([txt1.value,txt2.value,txt2.value])//e = [Txt1.value,txt2.value]Console.log ("message post to work")} Txt2.onchange=function() {_this.postmessage ([txt1.value,txt2.value,txt2.value])//e = [Txt1.value,txt2.value]Console.log ("message post to work")} _this.onmessage=function(s) {//received Data eResult.textcontent =S.data; }    }
function // e receiving parameters    of worker.postmessage transmission var s = (e.data[2]*e.data[1]);     var workerresult =  "result  :" + s;     //w Orker.onmessage The callback workerresult parameter }

Presumably, after looking at the above analysis, what are you going to do with the worker? For this problem, it is now possible to solve the non-blocking problem of threads, how to say that when the user changes the size of the browser and drags the browser, the main thread accesses the background data and does not interrupt the process between the data.

What are the browser that support worker?

Share a link caniuse, this tool you can more fully see the various browsers (hack).

* Note that the first letter of the worker must be in uppercase

* Note that the script directory of the worker must be the directory that the HTML can access

Full JS code access to GitHub

JavaScript: Understanding the Worker Event API

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.