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?
Advertisement
Answer
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."};