Expand my Community achievements bar.

Proper syntax for setTimeOut() inside a loop

Avatar

Level 7

Problem:

  I want to loop through something, say, 5 times. Each iteration requires the counter to be used. I can use a for loop to accomplish this task. Now, suppose I want to do the same thing, but I need to pause for a second between each attempt--or start each attempt on one second intervals.

What I've tried:

I have tried the following code in the click event for a button:

var s = tfExample.rawValue;

for (var i = 0; i<s.length; i++){

          app.setTimeOut("xfa.form.form1.variables.scripts.check(i)",1000);

}

And this is the code listed in scripts:

function check(i){

          p1.tfCounter.rawValue = i;

          p1.tfBefore.rawValue = s.substr(0,i);

          p1.tfCurrent.rawValue = s.substr(i,1);

          p1.tfAfter.rawValue = s.substr(i+1);

}


When I use this code, and preview in LC, LC just kind of stutters and does nothing; reader does nothing at all; and Acrobat throws an error:

ReferenceError: i is not defined

1:Doc:Exec

I presume that the "1" means line 1 from the source. That line is tfCounter.rawValue = i;. So, it looks to me like it is defined as it is declared in the function definition as a passed variable. I can't find any other mention of functions that shows using check(var i) or something to that effect in LC documentation. Any ideas?

I should probably also mention that I have tried putting that code in the same event with the button, and it doesn't work there, either. It's a different error message.

Message was edited by: jasotastic81: added other thing I've done and corrected a typo.

1 Reply

Avatar

Level 10

Hi,

The statement passed into the setTimeOut function is executed in the context of the Doc object, which doesn't have an i property so that is why you get the error.

Try;

app.setTimeOut("xfa.form.form1.variables.scripts.check("+i+")",1000);

Which will pass the value of i (from the click event) to the statement to be executed.

I think the "1" in your message refers to the line number of the statement passed to setTimeOut, so the actual "xfa.form.form1.variables.scripts.check(i)" bit.

Regards

Bruce