Say I have 5 different javascript timers that start ticking when I click “start”, each are referenced in variable timer1, timer2, timer3, timer4, timer5.
If these timers were each in an array, I would have to do something like this in pseudocode:
foreach timer in timerarray {
timer.stop;
timer.setstoptime(time that it stopped);
}
The issue here is that if I were to get “getstoptime”, due to having to iterate through the array for each timer, all the timers would have different values for the time that they stopped. Is there some way to keep track of all the timers and make it such that when I click on stop, if I were to be able to retrieve their stop time, all of them would be identical?
I know this is unlikely to happen with javascript iterating through arrays very fast, but because the array has to iterate, there is a small time gap before each timer stops.
I am looking for a way to ensure that all get “stop” executed on them simultaneously. If someone can provide some kind of sample code and some recommendation how to ensure this happens would be great.
Whether you are using JS or not there isn’t any way to do what you are asking. If you were able to use threads you’d still be unable to guarantee that all your timers would stop at the same instant.
If you want them to all have the same stop time why not get that value once before your loop starts and then explicitly set that value across all the timers.