Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why setTimeOut not working?

Enthusiast ,
Dec 08, 2023 Dec 08, 2023
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
TOPICS
Acrobat SDK and JavaScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 08, 2023 Dec 08, 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("processP

...
Translate
Community Expert ,
Dec 08, 2023 Dec 08, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 08, 2023 Dec 08, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2023 Dec 08, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 08, 2023 Dec 08, 2023

Oh, sometime. it still not ok.

var p=0;
var link_info="";
function processPage()
{
if (p < 10)
{
link_info += "page:"+p;
p++;
var t=app.setTimeOut("processPage()", 1000);
console.println(p);
}
else
{
event.value = link_info;
}
}

processPage();

 

Output: 
1

undefined2
3
4
5
6

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2023 Dec 08, 2023
LATEST

I ran it from the Console and it worked fine. Where did you place the code, exactly?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines