简体   繁体   English

javascript中的线程(或类似的东西)

[英]Threads (or something like) in javascript

I need to let a piece of code always run independently of other code. 我需要让一段代码始终独立于其他代码运行。 Is there a way of creating a thread in javascript to run this function? 有没有办法在javascript中创建一个线程来运行这个功能?

--why setTimeout doesn't worked for me - 为什么setTimeout对我不起作用

I tried it, but it runs just a single time. 我尝试过,但它只运行一次。 And if I call the function recursively it throws the error "too much recursion" after some time. 如果我以递归方式调用该函数,它会在一段时间后抛出“过多的递归”错误。 I need it running every 100 milis (it's a communication with a embedded system). 我需要每100毫秒运行一次(这是与嵌入式系统的通信)。

--as you ask, here goes some code - 如你所知,这里有一些代码

function update(v2) {
     // I removed the use of v2 here for simplicity
     dump("update\n"); // this will just print the string
     setTimeout(new function() { update(v2); }, 100); // this try doesn't work
}
update(this.v);

It throws "too much recursion". 它抛出“过多的递归”。

I am assuming you are asking about executing a function on a different thread. 我假设你问的是在不同的线程上执行一个函数。 However, Javascript does not support multithreading. 但是,Javascript不支持多线程。

See: Why doesn't JavaScript support multithreading? 请参阅: 为什么JavaScript不支持多线程?

The Javascript engine in all current browsers execute on a single thread. 所有当前浏览器中的Javascript引擎都在单个线程上执行。 As stated in the post above, running functions on a different thread would lead to concurrency issues. 如上面的帖子所述,在不同的线程上运行函数会导致并发问题。 For example, two functions modifying a single HTML element simultaneously. 例如,两个函数同时修改单个HTML元素。

As pointed out by others here, perhaps multi-threading is not what you actually need for your situation. 正如其他人所指出的,也许多线程并不是您实际需要的。 setInterval might be adequate. setInterval可能就足够了。

However, if you truly need multi-threading, JavaScript does support it through the web workers functionality. 但是,如果您真的需要多线程,JavaScript会通过Web worker功能支持它。 Basically, the main JavaScript thread can interact with the other threads (workers) only through events and message passing (strings, essentially). 基本上,主JavaScript线程只能通过事件和消息传递(字符串,本质上)与其他线程(工作者)进行交互。 Workers do not have access to the DOM. 工作人员无权访问DOM。 This avoids any of the concurrency issues. 这可以避免任何并发问题。

Here is the web workers spec: http://www.whatwg.org/specs/web-workers/current-work/ 以下是网络工作者规范: http//www.whatwg.org/specs/web-workers/current-work/

A more tutorial treatment: http://ejohn.org/blog/web-workers/ 更多教程处理: http//ejohn.org/blog/web-workers/

window.setTimeout()是你需要的。

Get rid of the new keyword for the function you're passing to setTimeout() , and it should work. 摆脱你传递给setTimeout()的函数的new关键字,它应该工作。

function update(v2) {
 try {
     dump("update\n");
 } catch(err) {
     dump("Fail to update " + err + "\n");
 }
 setTimeout(function() { update(v2); }, 100);
}
update(this.v);

Or just use setInterval() . 或者只使用setInterval()

function update(v2) {
 try {
     dump("update\n");
 } catch(err) {
     dump("Fail to update " + err + "\n");
 }
}
var this_v = this.v;
setInterval(function() {update(this_v);}, 100);

EDIT: Referenced this.v in a variable since I don't know what the value of this is in your application. 编辑:引用this.v的变量,因为我不知道是什么的价值this是在您的应用程序。

maybe you should to view about the javascirpt Workers (dedicated Web Workers provide a simple means for web content to run scripts in background threads), here a nice article, which explain how this works and how can we to use it. 也许您应该查看javascirpt Workers(专用Web Workers为Web内容提供了一种在后台线程中运行脚本的简单方法),这里有一篇很好的文章,解释了它是如何工作的以及我们如何使用它。 HTML5 web mobile tutororial HTML5网络移动教师

你可以尝试循环而不是递归

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

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