Comment: Many people always experience exceptions when testing the worker api, and they cannot test the results at all. It should be noted that simple text files cannot implement the worker. The actual code you write must be deployed to the server (tomcat.jBoss, etc.) to run the worker api.
Many people always experience exceptions when testing the worker API, and the test results are not effective at all.There is a thing that must be paid attention to when using worker, that is, simple text files cannot implement worker, and the actual code you write must be deployed to the server (tomcat.jBoss, etc.) to run the worker API.
Here is a simple example
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 type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
if(typeof(Worker)!=="undefined"){
console.log("zhichi work");
}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>
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 (usually used json to transmit in actual applications).