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.

有帮助吗?

解决方案 2

Use setInterval instead of setTimeout.

Also your while loop is not needed.

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

其他提示

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top