4

我正在尝试网络工作者让我的计算量大的 Javascript 真正并行。但是,我有一个问题,我的所有 javascript 文件都是 AMD 模块。我可以在我的网络工作者中使用 requirejs 吗?如何?

4

1 回答 1

7

在 Web Worker 中,可以使用importScripts函数加载其他脚本。RequireJS的源代码显示也支持 Web Worker:

// Line 23:
isWebWorker = !isBrowser && typeof importScripts !== 'undefined',
// Line 1877:
} else if (isWebWorker) {
    //In a web worker, use importScripts. This is not a very
    //efficient use of importScripts, importScripts will block until
    //its script is downloaded and evaluated. However, if web workers
    //are in play, the expectation that a build has been done so that
    //only one script needs to be loaded anyway. This may need to be
    //reevaluated if other use cases become common.
    importScripts(url);
于 2012-07-09T21:14:06.420 回答