admin管理员组文章数量:1287514
I have a WebWorker which I start with
new Worker("worker.js");
in this worker I try to load a needed JavaScript file without success via
self.importScripts("NeededJs.js");
const m = new NeededJs();
How can I solve this issue and include the needed JavaScript File in my WebWorker ?
Edit:
I get the error msg :
Uncaught ReferenceError: NeededJs is not defined
Thanks
This is the File I need to include File I need to Inlcude
I have a WebWorker which I start with
new Worker("worker.js");
in this worker I try to load a needed JavaScript file without success via
self.importScripts("NeededJs.js");
const m = new NeededJs();
How can I solve this issue and include the needed JavaScript File in my WebWorker ?
Edit:
I get the error msg :
Uncaught ReferenceError: NeededJs is not defined
Thanks
This is the File I need to include File I need to Inlcude
Share Improve this question edited May 22, 2018 at 5:16 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Dec 11, 2017 at 16:27 Zca89Zca89 911 gold badge1 silver badge3 bronze badges 9- Define "without success". Do you mean you get an exception? What is the exception? Do you mean you get no error but something else doesn't work? Please provide more details. – zero298 Commented Dec 11, 2017 at 16:34
-
Just to be sure... The "NeededJs.js" file contains a "global" available class called
NeededJs
? If not, does it include someting likethis.NeededJs = NeededClass
? – Siggy Commented Dec 11, 2017 at 16:39 - please have a look at the link i posted. – Zca89 Commented Dec 11, 2017 at 16:41
-
Just to state the obvious -
NeededJs
isnt defined in your file. So the errorUncaught ReferenceError: NeededJs is not defined
is correct. – Jamiec Commented Dec 11, 2017 at 16:45 - 1 Your question doesn't match exactly with what you're trying to do. You're trying to load a Node.js module from a Worker. To do that you would need to look into some library like browserify or similar. What I'd do instead is to download your own copy of the library, get rid of the load function and other stuff, and just leave the WebMonkeys constructor. Then just load your trimmed library from your worker. I tested it and I can guarantee you it works (btw, you need to enable OffscreenCanvas in your browser if it's disabled). – Diego Pino Commented Dec 11, 2017 at 17:33
1 Answer
Reset to default 8Yes, it is possible. You should note its a global function so remove the self.
from: https://developer.mozilla/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
Worker threads have access to a global function, importScripts(), which lets them import scripts. It accepts zero or more URIs as parameters to resources to import; all of the following examples are valid:
and
Note: Scripts may be downloaded in any order, but will be executed in the order in which you pass the filenames into importScripts() . This is done synchronously; importScripts() does not return until all the scripts have been loaded and executed.
Also, as you are using the module pattern for your imported file you need to pass an option to specify that:
from: https://developer.mozilla/en-US/docs/Web/API/Worker/Worker
new Worker("worker.js", {type:"module"});
本文标签: web workerIs it possible to load JavaScript file in a WebWorkerStack Overflow
版权声明:本文标题:web worker - Is it possible to load JavaScript file in a WebWorker? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248947a2365429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论