Skip to main content
Inspiring
May 1, 2015
Question

JS For Loop in Advanced Actions

  • May 1, 2015
  • 2 replies
  • 2924 views

Using Captivate 8.01.

If I place the following code to "Execute JavaScript" for the "On Success" property of a button works fine. But if I try to add it to an Advanced Action, it doesn't take the code unless I remove the For loop. This is just a simple timer loop. Don't understand what's going on here. Any ideas? Thanks.

var inc =0;

var inc1 = window.cpAPIInterface.getVariableValue('countNo'); // Reads Cp variable

inc1 = Number(inc1);

inc1 = inc1 + 1;

// For loop

for (var i = 0; i < 50000000; i++) {

        inc = inc + 1;

}

window.cpAPIInterface.setVariableValue('countNo', inc1);  // Returns Cp variable

This topic has been closed for replies.

2 replies

Participant
December 21, 2015

Thought I would reply, even though a solution has been provided. I had the same problem and a clue re the > sign working (Javascript For Loop not working in Execute Advanced Actions - Execute Javascript?) gave me the solution to using the loops in the Execute JavaScript action. I thought I would share it in case others wanted the solution.

Basically, count backwards.

var total = 0;

for(i=5; i>0; i--){

   total+=i;

}

total = 15

TLCMediaDesign
Inspiring
May 2, 2015

It may be working but you'd never know because you are incrementing inc 50 million times,before setting inc1

You should use a setTimeout function instead as it is actual time in milliseconds.

Window setTimeout() Method

met547Author
Inspiring
May 4, 2015

Good point. Thanks for the tip. But my original questions was that I was not able to add the code to an Advanced Action (Execute JavaScript). The Script window won't take the code with the For loop in it. If I place the same code to "Execute JavaScript" for the "On Success" property of a button it works just fine, I can place the code with the For loop.

TLCMediaDesign
Inspiring
May 4, 2015

Put the code in a function in the HTML and call the function.