My task involves managing a list of events that each have a specific timestamp
. I am looking to display these events in the order of their timestamps.
To introduce a delay between events:
delay = timestamp(t+1) - timstamp(t)
I understand that implementing this with setTimeout
may not be ideal, especially when the delay is not constant. However, there might be a workaround available for situations where the timeout remains constant, which is not the case for me.
Is it feasible to ensure that the next setTimeout()
only triggers after the previous one has completed? For instance, if the first setTimeout()
has a 5-second delay and the second one 3 seconds, the latter will execute before the former. My goal is to maintain the order specified by the timestamps while ensuring sequential execution.
The example provided illustrates how this can work with a fixed delay, but my scenario requires dynamically calculating the delay based on the event data obtained from iterating through the list.
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(i);
}, 1000);
}