简体   繁体   English

在工作线程中使用setTimeOut吗?

[英]Using setTimeOut in a worker thread?

I'm trying to use a library that is realized by relying a lot on window.setTimeOut . 我正在尝试使用通过依赖window.setTimeOut实现的库。 I want to use it in a worker thread that doesn't have access to the window scope. 我想在无法访问window范围的工作线程中使用它。

Is it possible to use a polyfill or use any other method that does the same thing within this context? 在这种情况下,是否可以使用polyfill或其他可做相同事情的方法?

You can use setTimeout ( not 您可以使用setTimeout setTimeOut ) perfectly well. ) 非常好。 Just use it as global variable: 只需将其用作全局变量即可:

setTimeout( ... stuff stuff stuff ..., number);

And if you want to use global scope object for some reason, self is defined both in Browser and Web Worker: 并且,如果出于某种原因要使用全局范围对象,则在浏览器和Web Worker中都定义了self

self.setTimeout( ... stuff stuff stuff ..., number);

If a library is trying to access window , just define it to be global scope: 如果库正在尝试访问window ,只需将其定义为全局范围:

self.window = self;
window.setTimeout( ... stuff stuff stuff ..., number);

If you have further doubts, please check this jsfiddle: https://jsfiddle.net/ryovLea1/1/ 如果您还有其他疑问,请检查以下jsfiddle: https ://jsfiddle.net/ryovLea1/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM