0

I have a while loop where I look for two attributes in the array, if they're not there, then I invoke the sleep function for 10 seconds and look for those attributes again. I want there to be an Elapsed time there so that the user can see how long we've been looking for those attributes.

var flag1 = "false";
var flag2 = "false";

while (flag1 == "false" || flag2 == "false")
   for { //for loop where it looks for some attributes in an array and if found, changes the respective flag to true
   }

   //if conditions so if the flags are still false then prints not found and tells that it will check again
   if (flag1 == "false") {
       print ("Attribute 1 not found. Check again in 10 seconds");
   }
   if (flag2 == "false") {
       print ("Attribute 2 not found. Check again in 10 seconds");
   }
   
   //Stopwatch 
   var startTime = new Date();
   sleep (10000);
   var endTime = new Date();
   var timeDiff = endTime - startTime;
   timeDiff /= 1000;
   var seconds = Math.round(timeDiff % 60);
   print("Elapsed Time is " + seconds + "seconds");
}
print("Both attributes found."};

Expected output:

Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 0 seconds.

//after 10 seconds.
Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 10 seconds.

//after another 10 seconds.
Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 20 seconds.

//after another 10 seconds and assuming that the attribute was found in array this time.
Both attributes found.

Current Ouptut

Attribute 1 not found. Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed Time is 10seconds
Attribute 1 not found. Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed Time is 10seconds

The Elapsed time when printed always shows 10 seconds, I want it to keep incrementing. How would I do that?

  • Move the `startTime` outside the loop? – Heretic Monkey Jan 20 '21 at 14:43
  • @HereticMonkey Thanks that does help but in the case where both attributes are found in the first iteration itself, it is still going to print Elapsed Time is .... How do I counter that? I don't want it to `sleep(1000)` or print `elapsed time` if both attributes are found in the first iteration. – confusedreally Jan 20 '21 at 14:50
  • What does `sleep (10000)` do? Maybe see [*Sleep in JavaScript - delay between actions*](https://stackoverflow.com/questions/758688/sleep-in-javascript-delay-between-actions). – RobG Jan 20 '21 at 21:47

1 Answers1

1

Just move startTime declaration

var startTime = new Date();

before the while loop:

var startTime = new Date();

while (flag1 == "false" || flag2 == "false")
   .... 


   if (flag1 && flag2) {
     // if your condition is matched, exit the loop
     break;
   }
   //Stopwatch 
   sleep (10000);
   var endTime = new Date();
   var timeDiff = endTime - startTime; 
   timeDiff /= 1000;
   var seconds = Math.round(timeDiff % 60);
   print("Elapsed Time is " + seconds + "seconds");
}
print("Both attributes found."};
   
Dario
  • 6,152
  • 9
  • 39
  • 50
  • Thanks that does help but in the case where both attributes are found in the first iteration itself, it is still going to print `Elapsed Time is ...`. How do I counter that? – confusedreally Jan 20 '21 at 14:49
  • Just check your condition before `sleep` and in case it is true, exit the loop with a `break ` statement; check updated code – Dario Jan 20 '21 at 15:00
  • Thanks that helped! Also, I see that if the `Elapsed time` is over 60 seconds, it restarts again from 0 seconds. I want it to continue incrementing from 60 seconds. How do I do that? – confusedreally Jan 20 '21 at 15:48
  • Try calculating the date diff with `var seconds = (endDate.getTime() - startDate.getTime()) / 1000;`, check this thread also https://stackoverflow.com/questions/13894632/get-time-difference-between-two-dates-in-seconds – Dario Jan 20 '21 at 16:24
  • Thanks that also did the job! But now it prints decimals too.. like `100.323 seconds` `10.321 seconds`. I just want integer. How do I fix that? – confusedreally Jan 20 '21 at 19:08
  • Hi, any idea on how to remove the decimals? – confusedreally Jan 20 '21 at 21:54
  • `Math.trunc()` function returns the integer part of a number by removing any fractional digits - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc – Dario Jan 21 '21 at 08:22