admin管理员组文章数量:1323342
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?
--why setTimeout doesn't worked for me
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 munication with a embedded system).
--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 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?
--why setTimeout doesn't worked for me
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 munication with a embedded system).
--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".
Share Improve this question edited Mar 7, 2012 at 13:31 munity wiki12 revs, 6 users 42%
Tom Brito 6
- I'd like to see some code :) maybe something can be improved... – fcalderan Commented Nov 16, 2010 at 13:46
- ok, 1) what dump() does? 2) why do you pass an argument if you don't use? 3) what try/catch is trying to catch? =) 4) Why do you create an instance of a function? if you remove the 'new' constructor? What the purpose of this code? – fcalderan Commented Nov 16, 2010 at 14:02
- 1) dump() just prints the string. 2) I'll use the argument, I removed the use for simplicity. 3) same as 2. 4) this was I try use a sample code, I don't know how to make this keep running. – Student Commented Nov 16, 2010 at 14:08
- @fcalderan answered. But this sample code should just print "update" every 100ms. That's what I'm trying to do now. – Student Commented Nov 16, 2010 at 14:12
- with a simplified version the problem could be not visible... anyway start to remove the 'new' constructor and see if this work better. Then replace the try catch with another kind of control. But without seeing the real code it's diffcult to help you. – fcalderan Commented Nov 16, 2010 at 14:13
6 Answers
Reset to default 3I am assuming you are asking about executing a function on a different thread. However, Javascript does not support multithreading.
See: Why doesn't JavaScript support multithreading?
The Javascript engine in all current browsers execute on a single thread. 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.
As pointed out by others here, perhaps multi-threading is not what you actually need for your situation. setInterval might be adequate.
However, if you truly need multi-threading, JavaScript does support it through the web workers functionality. Basically, the main JavaScript thread can interact with the other threads (workers) only through events and message passing (strings, essentially). Workers do not have access to the DOM. This avoids any of the concurrency issues.
Here is the web workers spec: http://www.whatwg/specs/web-workers/current-work/
A more tutorial treatment: http://ejohn/blog/web-workers/
Get rid of the new
keyword for the function you're passing to setTimeout()
, and it should work.
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()
.
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.
window.setTimeout() is what you need.
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. HTML5 web mobile tutororial
U can try a loop instead of recursivity
本文标签: multithreadingThreads (or something like) in javascriptStack Overflow
版权声明:本文标题:multithreading - Threads (or something like) in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135363a2422343.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论