Home > Article > Web Front-end > html5 worker example (1) Why the test fails to work
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)
function messageHandler(e) { postMessage("worker says: " + e.data + " too"); } addEventListener("message", messageHandler, true); postMessage("2222222222");
html code index.html
<!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 above is the html5 worker example (1) why the test cannot be effective, please pay attention to more related content PHP Chinese website (www.php.cn)!