5

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 communication 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".

The Student
  • 27,520
  • 68
  • 161
  • 264
  • I'd like to see some code :) maybe something can be improved... –  Nov 16 '10 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? –  Nov 16 '10 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. – The Student Nov 16 '10 at 14:08
  • @fcalderan answered. But this sample code should just print "update" every 100ms. That's what I'm trying to do now. – The Student Nov 16 '10 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. –  Nov 16 '10 at 14:13
  • @Tom - See my answer. Removing `new` fixed it. Also gave a `setInterval` example. – user113716 Nov 16 '10 at 14:15

6 Answers6

4

I 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.

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
3

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.org/specs/web-workers/current-work/

A more tutorial treatment: http://ejohn.org/blog/web-workers/

jhurshman
  • 5,861
  • 2
  • 26
  • 16
2

window.setTimeout() is what you need.

BarsMonster
  • 6,483
  • 2
  • 34
  • 47
  • Yeah, setInterval() would be what you need. – はると Nov 16 '10 at 13:37
  • I tried it, it works just a single time. And if I call the function recursively it throws the error "too much recursion" after some time. – The Student Nov 16 '10 at 13:38
  • 2
    Why is this what the OP needs? It just calls a piece of code after a certain period. Which is not an independently running piece of code or a thread... – Veger Nov 16 '10 at 13:39
  • @Veger that's what I want, to call the piece of code on every 100ms. How can I do that? The setTimeout with recursive function throws an "too much recursion" error. – The Student Nov 16 '10 at 13:48
2

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.

user113716
  • 318,772
  • 63
  • 451
  • 440
1

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

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Gio
  • 41
  • 6
0

U can try a loop instead of recursivity

Pau
  • 803
  • 1
  • 6
  • 12