I am trying to create a timed loop which iterates through an array of objects and runs a timer based on a key:value in each object. For simplicity I have only called a console log in the functions. The steps I need to happen are:
Here is the code without any timers:
const list = [{
item: 'Item 1',
time: 30
},
{
item: 'Item 2',
time: 25
},
{
item: 'Item 3',
time: 40
}
];
const wait = 5;
function logger(input, suffix) {
console.log(`${input} ${suffix}`)
}
function runTimer() {
logger(list[0].item, 'timer')
for (let i = 0; i < list.length; i++) {
logger(list[i].item, 'timer');
for (t = list[i].time; t > 0; t--) {
logger(t, 'seconds');
}
for (let w = wait; w > 0; w--) {
logger(w, 'secs wait')
}
}
};
runTimer();
I can get the log order correct, but I am struggling to get the timing right, I am getting lost in calling setTimeouts within multiple timed loops. How can I do that?
I would suggest you to use chained Promises to implement this. You could start from creating a general countdown function that returns a Promise:
function countdown(time, msg) {
return new Promise(resolve => {
const i = setInterval(() => {
logger(time, msg);
if (time === 0) { // the promise resolves once the time reaches 0
clearInterval(i);
resolve();
}
time--;
}, 1000);
});
}
Then you could use this function to combine each item's countown with the waiting time:
function runTimer () {
let p = Promise.resolve();
for (let i of list) {
p = p.then(() => countdown(i.time, i.item).then(() => countdown(wait, 'wait')));
}
p.then(() => console.log('done!'));
};
Here is the full example:
const list = [
{
item:'Item 1',
time:3
},
{
item:'Item 2',
time:5
},
{
item:'Item 3',
time:4
}
];
const wait = 5;
function logger(input, suffix) {
console.log(`${input} ${suffix}`)
}
function runTimer () {
let p = Promise.resolve();
for (let i of list) {
p = p.then(() => countdown(i.time, i.item).then(() => countdown(wait, 'wait')));
}
p.then(() => console.log('done!'));
};
function countdown(time, msg) {
return new Promise(resolve => {
const i = setInterval(() => {
logger(time, msg);
if (time === 0) {
clearInterval(i);
resolve();
}
time--;
}, 1000);
});
}
runTimer();
Hope this helps!
const list = [ {
item:'Item 1',
time:5
},
{
item:'Item 2',
time:3
},
{
item:'Item 3',
time:6
}
];
const wait = 5;
let itemCounter = 0;
runTimer();
function runTimer() {
// Timer for each item in list
console.log(list[itemCounter].item+ " timer");
var timer = list[itemCounter].time;
var interval = setInterval(function() {
var minutes = parseInt(timer / 60, 10),
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
console.log(minutes+ " : " +seconds);
if(timer-- <= 0) {
clearInterval(interval);
waitTimer();
}
}, 1000);
}
function waitTimer() {
// Timer for "wait time" after an Item timer is complete
var timer = wait;
console.log("Wait timer after "+list[itemCounter].item+" timer")
var interval = setInterval(function() {
var minutes = parseInt(timer / 60, 10),
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
console.log(minutes+ " : " +seconds);
if(timer-- <= 0) {
clearInterval(interval);
itemCounter++;
if(itemCounter < list.length) {
runTimer();
}
}
}, 1000);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With