Skip to main content
daitranthanhoa
Inspiring
December 8, 2023
Answered

Why setTimeOut not working?

  • December 8, 2023
  • 1 reply
  • 1238 views
Why setTimeOut not working?
This is my script:
 
var p=0;
var link_info="";
function processPage()
{
if (p < 10)
{
link_info += "page:"+p;
p++;
app.setTimeOut(processPage, 1000);
console.println(link_info);
}
else
{
event.value = link_info;
}
}
 
processPage();

 

 
Result output:
 
page:0
undefined
This topic has been closed for replies.
Correct answer try67

It's good it's not working, as you'll end up with an infinite loop of a function that calls itself within it...

I see two issues with it, beyond that:

- The first parameter needs to be a string, not a function. In your case it would be:

"processPage()"

- You should always use a variable to handle the return object of setTimeOut. If you don't do that it often doesn't work, even if you don't use that variable for anything later on. So the complete line should be:

var to1 = app.setTimeOut("processPage()", 1000);

 

But again, this will lead to an infinite loop. What exactly are you trying to achieve with this code?

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 8, 2023

It's good it's not working, as you'll end up with an infinite loop of a function that calls itself within it...

I see two issues with it, beyond that:

- The first parameter needs to be a string, not a function. In your case it would be:

"processPage()"

- You should always use a variable to handle the return object of setTimeOut. If you don't do that it often doesn't work, even if you don't use that variable for anything later on. So the complete line should be:

var to1 = app.setTimeOut("processPage()", 1000);

 

But again, this will lead to an infinite loop. What exactly are you trying to achieve with this code?

daitranthanhoa
Inspiring
December 8, 2023

Thank you, it is ok. My func will finish by p++;

try67
Community Expert
Community Expert
December 8, 2023

Ah yes, since p is defined outside the function it should be OK...