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

JS For Loop in Advanced Actions

Participant ,
May 01, 2015 May 01, 2015

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

2.9K
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
People's Champ ,
May 01, 2015 May 01, 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

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
Participant ,
May 04, 2015 May 04, 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.

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
People's Champ ,
May 04, 2015 May 04, 2015

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

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 Beginner ,
Dec 21, 2015 Dec 21, 2015
LATEST

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

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
Resources
Help resources