Skip to content
Advertisement

How do I keep incrementing the Elapsed time variable while its in the while loop instead of starting from 0 seconds everytime

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.

JavaScript

Expected output:

JavaScript

Current Ouptut

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement