I'm looping over an array using a "for" loop. I need to go through the loop at 2 second intervals. Need to stop the loop if condition is met. but I can't stop the loop because it has "setTimeout".
let selects = [1,2,3,4,5,6,7,8,9];
for (let j =0; j < selects.length; j++) {
task(j);
if (j===5) { break; }
}
function task(i) {
let tasker = setTimeout(function() {
console.log("loop " + i)
}, 2000 * i);
}