未捕获的ReferenceError:未定义importScripts

时间:2022-07-15 15:18:14

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/

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

我正在使用https://code.google.com/p/bitjs/,它始于

importScripts('io.js');
importScripts('archive.js');

3 个解决方案

#1


13  

This code needs to be inside a worker script. The worker itself is created via a new Worker object - see Getting Started in the tutorial.

此代码需要在worker脚本中。工作程序本身是通过新的Worker对象创建的 - 请参阅教程中的入门。

The code you've linked is inside the worker created here.

您链接的代码位于此处创建的工作程序中。

#2


22  

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.

当你创建一个worker时,它实际上被执行了两次。第一遍是在全局“窗口”对象的上下文中(意味着您可以访问所有窗口对象函数)。第二个调用是在具有不同全局对象的worker的上下文中,其中存在“importScripts”。

// 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.

请注意,addEventListener位于if语句中。如果将它放在它之外,你的回调将被注册两次。一旦进入'窗口'全球,一旦进入工人的全球。

Happy coding!

快乐的编码!

#3


7  

I encountered this error as well. In my case, it is because I am testing the code using Karma/Jasmine. Due to the test framework, the worker.js file is loaded by main thread as well.

我也遇到了这个错误。在我的情况下,这是因为我正在使用Karma / Jasmine测试代码。由于测试框架,worker.js文件也由主线程加载。

I avoided this error by wrappig the worker.js file with:

我通过wrappig worker.js文件避免了这个错误:

    if( 'undefined' === typeof window){
       importScripts('workerscript2.js');
    ...
    }

Please refer to the comment below by Rob, which offers an alternative solution.

请参阅Rob的下面评论,它提供了另一种解决方案。

#1


13  

This code needs to be inside a worker script. The worker itself is created via a new Worker object - see Getting Started in the tutorial.

此代码需要在worker脚本中。工作程序本身是通过新的Worker对象创建的 - 请参阅教程中的入门。

The code you've linked is inside the worker created here.

您链接的代码位于此处创建的工作程序中。

#2


22  

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.

当你创建一个worker时,它实际上被执行了两次。第一遍是在全局“窗口”对象的上下文中(意味着您可以访问所有窗口对象函数)。第二个调用是在具有不同全局对象的worker的上下文中,其中存在“importScripts”。

// 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.

请注意,addEventListener位于if语句中。如果将它放在它之外,你的回调将被注册两次。一旦进入'窗口'全球,一旦进入工人的全球。

Happy coding!

快乐的编码!

#3


7  

I encountered this error as well. In my case, it is because I am testing the code using Karma/Jasmine. Due to the test framework, the worker.js file is loaded by main thread as well.

我也遇到了这个错误。在我的情况下,这是因为我正在使用Karma / Jasmine测试代码。由于测试框架,worker.js文件也由主线程加载。

I avoided this error by wrappig the worker.js file with:

我通过wrappig worker.js文件避免了这个错误:

    if( 'undefined' === typeof window){
       importScripts('workerscript2.js');
    ...
    }

Please refer to the comment below by Rob, which offers an alternative solution.

请参阅Rob的下面评论,它提供了另一种解决方案。