Html5 worker instance (I) Why cannot I test it?

Source: Internet
Author: User

Comments: When testing the worker api, many people always encounter exceptions and cannot test the effect at all. Note that a simple text file cannot implement worker, the actual code you write must be deployed on the server (tomcat. jBoss, etc.) to run the worker api many people always encounter exceptions when testing the worker api, and the test results are not effective at all.
When using worker, you must note that a simple text file cannot implement worker. The actual code you write must be deployed on the server (tomcat. jBoss, etc.) to run the worker api.
Below is a simple example.

Js Code test. js (worker)

The Code is as follows:
Function messageHandler (e ){
PostMessage ("worker says:" + e. data + "too ");
}
AddEventListener ("message", messageHandler, true );
PostMessage ("2222222222 ");

Html code index.html

The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Title> index.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
</Head>
<Body>
<Script type = "text/javascript">
If (typeof (Worker )! = "Undefined "){
Console. log ("zhichi worke ");
} Else {
Console. log ("no support! ");
}
Function messageHandler (e ){
Console. log (e. data );
}
Function errorHandler (e ){
Console. log (e. message, e );
}
Var myWorker = new Worker ("task. js ");
MyWorker. addEventListener ("message", messageHandler, true );
MyWorker. addEventListener ("error", errorHandler, true );
MyWorker. postMessage ("1 fangsong d ");
</Script>
</Body>
</Html>

The index.html page is directly merged, and then the string sent by the worker is displayed on the console Panel of the browser (json transfer is generally used in actual applications ).

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.