Domanda

I'm trying to create a new scheduled report and I've one doubt about it: How can I create a script on it with a loop that runs a function every 10 seconds? Something like:

var value = 1;
while(value > 0){
    setTimeout(function(){myFunction()},10000);
    value = value -1;
}

When I just run my report into the report studio (without schedule) this script executes successfully, but after the schedule it doesn't work anymore. Someone know why is this happening or have any other idea?

Thanks in advance.

È stato utile?

Soluzione 2

Use setInterval instead of setTimeout.

Also your while loop is not needed.

Just use this instead: setInterval(myFunction, 10000);

Altri suggerimenti

If you want to keep the same structure, you can use setTimeout to make it slightly recursive:

var repeatedFunction = function(){
  // Do something
  setTimeout(repeatedFunction, 10 * 1000);
};

but you're better off using setInterval:

setInterval(function(){
  // do something
}, 10 * 1000);

and if you need to cancel it, store the interval:

var repeatedFunction = setInterval(function(){
  // do something
}, 10 * 1000);

// .. something happened; need to cancel
clearTimeout(repeatedFunction);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top