Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why do I keep getting this error?

I should be able to use this global function right?

http://www.html5rocks.com/en/tutorials/workers/basics/

I'm using chrome.

I'm using https://code.google.com/p/bitjs/ and it begins with

importScripts('io.js');
importScripts('archive.js');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

When you create a worker it is actually executed twice. The first pass is in the context of the global 'window' object(meaning you have access to all the window object functions). The second call through is in the context of the worker which has a different global object, one where 'importScripts' exists.

// proper initialization
if( 'function' === typeof importScripts) {
   importScripts('script2.js');
   addEventListener('message', onMessage);

   function onMessage(e) { 
     // do some work here 
   }    
}

Notice the addEventListener is inside the if statement. If you place it outside of it, your callback will be registered twice. Once on the 'window' global and once on the worker's global.

Happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[5] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...