天天看點

html5 Worker學習

HTML:

mainthread.JS

thread1.js

thread2.js

運作結果:

html5 Worker學習

注意的是要釋出到伺服器才能運作否則出現:

Uncaught SecurityError: Failed to construct ‘Worker‘: Script at ‘xxxx‘ cannot be accessed from origin

‘null‘.

總結 :

1:建立worker執行個體并傳遞對應的js檔案

2:發送消息  postMessage() 

3:監聽消息 onmessage

4:停止terminate()方法

5:onerror錯誤異常

監聽消息也可以用下列方式.