Skip to main content
daitranthanhoa
Inspiring
Dec 8, 2023 9:57 AM UTC
Answered

Why setTimeOut not working?

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Dec 8, 2023 10:03 AM UTC

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?

5 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Dec 8, 2023 10:03 AM UTC

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?

Translate
daitranthanhoa
Inspiring
Dec 8, 2023 10:21 AM UTC

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

Translate
try67
Community Expert
Community Expert
Dec 8, 2023 10:27 AM UTC

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

Translate