编辑你的代码:
<!DOCTYPE html> <html> <body> <p>计数: <output id="result"></output></p> <button onclick="startWorker()">开始工作</button> <button onclick="stopWorker()">结束工作</button> <br><br> <script> var w; function startWorker() { if(typeof(Worker)!=="undefined") { if(typeof(w)=="undefined") { w=new Worker("/static/js/demo_workers.js"); } w.onmessage = function (event) { document.getElementById("result").innerHTML=event.data; }; } else { document.getElementById("result").innerHTML="对不起, 你的浏览器不支持Web Workers..."; } } function stopWorker() { w.terminate(); } </script> </body> </html>
查看结果:
phpxuexi.net
- 亲自试一试