//index.html $('#test').click(function () { alert(1); }); var worker = new Worker("task.js");单独开一个线程 worker.onmessage = function(event){ // 消息文本放置在data属性中, alert(event.data); } worker.postMessage(10000000); // task.js onmessage = function(event) { var num = event.data; var result = 0; for (var i = 0; i < num; i++) { result += i; } // 向线程创建源送回消息 postMessage(result); } // index.html,共享线程 var worker = new SharedWorker('task9.js'); worker.port.addEventListener('message', function(e) { console.log(e.data); }, false); worker.port.start(); worker.port.postMessage('100'); // test.html共享线程 var worker = new SharedWorker('task9.js'); worker.port.addEventListener('message', function(e) { alert(e.data); }, false); worker.port.start(); worker.port.postMessage('get'); //task9.js var result = 0; onconnect = function(e) { var port = e.ports[0]; port.postMessage(' connection sucess '); port.onmessage = function(e) { if (e.data !== "get") { for (var i = 0; i < e.data; i++) { result += i; } } port.postMessage(result); }; };