Themes (or something like) in javascript

I need a piece of code to always run independently of other code. Is there a way to create a stream in javascript to run this function?

- why setTimeout doesn't work for me

I tried, but it only works once. And if I call the function recursively, it will cause a "too much recursion" error after some time. I need it to work every 100 million (this is a message with an embedded system).

- as you ask, here is the 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 produces "too much recursion."

+5
source share
6 answers

new , setTimeout(), .

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

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: this.v , , this .

+2

, . Javascript .

: JavaScript ?

Javascript . , concurrency. , , HTML.

+4

, , , . setInterval .

, , JavaScript -. JavaScript () ( , ). DOM. concurrency.

-: http://www.whatwg.org/specs/web-workers/current-work/

: http://ejohn.org/blog/web-workers/

+3

window.setTimeout() - , .

+2

Maybe you should take a look at javascirpt Workers (dedicated web workers provide simple tools for web content to run scripts in the background thread), here is a good article that explains how it works and how we can use it. HTML5 web mobile tutororial

+1
source

U can try loop instead of recursion

0
source

All Articles