2017-12-11 25 views
0

나는 내가이 문제를 해결하고 필요한 자바 스크립트 파일을 포함 할 수있는 방법WebWorker에서 JavaScript 파일을로드 할 수 있습니까?

self.importScripts("NeededJs.js"); 
const m = new NeededJs(); 

를 통해 성공없이 필요한 자바 스크립트 파일을로드하려고 나는이 노동자에

new Worker("worker.js");

으로 시작할 WebWorker이 내 WebWorker에?

편집 :

나는 오류 MSG를 얻을 :

Uncaught ReferenceError: NeededJs is not defined

감사

이 내가 File I need to Inlcude

+0

"성공하지 않음"을 정의하십시오. 예외가 있다는 뜻입니까? 예외는 무엇입니까? 오류가 없지만 다른 것이 작동하지 않는다는 것을 의미합니까? 자세한 내용을 입력하십시오. – zero298

+0

그냥 ... "NeededJs.js"파일에는 "NeededJs"라는 "글로벌"사용 가능한 클래스가 포함되어 있습니까? 그렇지 않다면'this.NeededJs = NeededClass'와 같은 someting을 포함합니까? – Siggy

+0

게시 한 링크를 한번보십시오. – Zca89

답변

0

예, 그것은 가능하다 포함하는 데 필요한 파일입니다. 당신은 너무 제거 그에게 전역 함수에주의해야 self.

에서 : https://developer.mozilla.org/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:

또한

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.

, 당신은 당신이 통과해야 가져온 파일에 대한 모듈 패턴을 사용으로 옵션을 지정하는 옵션 :

출처 : https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker

new Worker("worker.js", {type:"module"}); 
+0

작동하지 않는다 Blob로 작업자를로드해야한다고 말해야합니다. – Zca89

+0

@ Zca89 귀하의 질문에 중요한 정보 일 것입니다. 너? – Jamiec

+0

예 사실 방금 알았습니다. – Zca89