문제

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