Home > Web Front-end > H5 Tutorial > html5 worker example (1) Why the test does not work_html5 tutorial skills

html5 worker example (1) Why the test does not work_html5 tutorial skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:49:28
Original
1578 people have browsed it

Many people always encounter exceptions when testing the worker API and cannot test the effect at all.
One thing you must pay attention to when using workers is that a simple text file cannot implement a worker. The actual code you write must be deployed to the server (tomcat.jBoss, etc.) to run the worker api.
Write a simple example below

js code test.js (worker)

Copy code
The code is as follows:

function messageHandler(e) {
postMessage("worker says: " e.data " too");
}
addEventListener("message", messageHandler, true);
postMessage("2222222222");

html code index.html

Copy code
The code is as follows:



< head>
index.html



-->


< ;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");


< /html>

Instead of directly accessing the index.html page afterwards, you will see the string sent by the worker in the console panel of the browser (json transmission is generally used in actual applications).
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
html5 validation for symfony 2.1
From 1970-01-01 08:00:00
0
0
0
The difference between HTML and HTML5
From 1970-01-01 08:00:00
0
0
0
html5 show hide
From 1970-01-01 08:00:00
0
0
0
Can PDF files run HTML5 and Javascript?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template