http://stackoverflow.com/questions/14500091/uncaught-referenceerror-importscripts-is-not-defined
http://www.html5rocks.com/en/tutorials/workers/basics/#toc-gettingstarted
Importing scripts and libraries
Worker threads have access to a global function, importScripts()
, which lets them import scripts or libraries into their scope. It accepts as parameters zero or more URIs to resources to import; all of the following examples are valid:
importScripts(); /* imports nothing */
importScripts('foo.js'); /* imports just "foo.js" */
importScripts('foo.js', 'bar.js'); /* imports two scripts */
Worker
构造函数的参数 URI 必须遵循 同源策略。The browser loads each listed script and executes it. Any global objects from each script may then be used by the worker. If the script can't be loaded,NETWORK_ERROR
is thrown, and subsequent code will not be executed. Previously executed code (including code deferred using window.setTimeout()
) will still be functional though. Function declarations after the importScripts()
method are also kept, since these are always evaluated before the rest of the code.
importScripts()
. This is done synchronously; importScripts()
does not return until all the scripts have been loaded and executed.
Web Workers and the DOM
Since web workers are in external files, they do not have access to the following JavaScript objects:
- The window object
- The document object
- The parent object
Uncaught ReferenceError: importScripts is not defined
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'); //importScripts API只能在worker中使用, archive.js文件中不能再使用importScripts,并且archive.js文件中不能使用 1.
- The window object 2.
- The document object 3.
- The parent object
This code needs to be inside a worker script. The worker itself is created via a new Worker
object - seeGetting Started in the tutorial.
The code you've linked is inside the worker created here.
=============
Features Available to Workers
Due to their multi-threaded behavior, web workers only has access to a subset of JavaScript's features:
- The
navigator
object - The
location
object (read-only) XMLHttpRequest
setTimeout()/clearTimeout()
andsetInterval()/clearInterval()
- The Application Cache
- Importing external scripts using the
importScripts()
method - Spawning other web workers
Workers do NOT have access to:
- The DOM (it's not thread-safe)
- The
window
object - The
document
object - The
parent
object