识别 JS 脚本何时作为 Worker 运行

发布于 2024-12-21 07:48:15 字数 139 浏览 1 评论 0原文

我有一个脚本,可以直接运行,也可以在浏览器中作为 Web Worker 运行。我想仅在作为工作人员运行时运行此脚本的一部分;所以我的问题是,脚本如何将自己标识为以这种方式运行?

我在规范中看不到任何允许这种情况发生的内容;我错过了一些明显的东西吗?

I have a script which can be run either directly or, when available in the browser, as a Web Worker. I'd like to run a portion of this script only when run as a worker; so my question is, how can a script identify itself as being run this way?

I can't see anything in the spec that would allow this to happen; am I missing something obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×眷恋的温暖 2024-12-28 07:48:15

下面:

<html>
<head>
<title>Worker</title>
</head>
<body>
</body>
<script >
  var w = new Worker ('worker.js');
  w.onmessage = function (e) {
    document.body.innerHTML += '<br>' + 'WORKER : ' + e.data;
  };
</script>
<script src='worker.js'></script>
</html>

worker.js 作为脚本和工作线程被调用。

worker.js 包含:

  var msg = 'postMessage is ' + postMessage.toString () + 
          ', self.constructor is ' + self.constructor;
  try {
    postMessage (msg);
  } catch (e) {
    document.body.innerHTML += '<br>SCRIPT : ' + msg;
  }  

在工作环境中,postMessage 成功,在脚本环境中失败,因为它未定义,或者在浏览器中需要第二个参数。

输出是:

chrome :

SCRIPT : postMessage is function () { [native code] }, self.constructor is function DOMWindow() { [native code] } 
WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function DedicatedWorkerContext() { [native code] }

firefox :

SCRIPT : postMessage is function postMessage() { [native code] }, self.constructor is [object Window]
WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function DedicatedWorkerGlobalScope() { [native code] }

opera:

WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function Object() { [native code] }
SCRIPT : postMessage is function postMessage() { [native code] }, self.constructor is function Object() { [native code] }

All under Ubuntu。

In the following :

<html>
<head>
<title>Worker</title>
</head>
<body>
</body>
<script >
  var w = new Worker ('worker.js');
  w.onmessage = function (e) {
    document.body.innerHTML += '<br>' + 'WORKER : ' + e.data;
  };
</script>
<script src='worker.js'></script>
</html>

worker.js is invoked both as a script and as a worker.

worker.js contains :

  var msg = 'postMessage is ' + postMessage.toString () + 
          ', self.constructor is ' + self.constructor;
  try {
    postMessage (msg);
  } catch (e) {
    document.body.innerHTML += '<br>SCRIPT : ' + msg;
  }  

In the worker environment, the postMessage succeeds, in the script environment it fails because either it is undefined or, in a browser, it requires a second argument.

Output is :

chrome :

SCRIPT : postMessage is function () { [native code] }, self.constructor is function DOMWindow() { [native code] } 
WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function DedicatedWorkerContext() { [native code] }

firefox :

SCRIPT : postMessage is function postMessage() { [native code] }, self.constructor is [object Window]
WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function DedicatedWorkerGlobalScope() { [native code] }

opera:

WORKER : postMessage is function postMessage() { [native code] }, self.constructor is function Object() { [native code] }
SCRIPT : postMessage is function postMessage() { [native code] }, self.constructor is function Object() { [native code] }

All under Ubuntu.

你是年少的欢喜 2024-12-28 07:48:15

我会使用 Modernizr (它是一个开源 JavaScript 库,可以帮助您避免一次又一次地重新发明轮子)。

if (Modernizr.webworkers) {
  // window.Worker is available!
} 
else {
  // no native support for web workers
}

I would use Modernizr (it is an open-source JavaScript library that helps you NOT to reinventing the wheel again and again).

if (Modernizr.webworkers) {
  // window.Worker is available!
} 
else {
  // no native support for web workers
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文