I'm needing a function that can loop through an array while accounting for delay, and not adding the delay all at once while going through an array.
My example of what I need:
const array = [ 0.06, 0.08, 0.04, 0.05, 0.06, 0.03 ];
loop(array); // this is the function
userInputForStop(); // my function where something causes the array to be deleted/set to []
I've tried:
const array = [ ""var array = [ "this", "is", "not", "going", "to", "work" ];
for (var i = 0; i < array.length; i++) {
(function (i) {
setTimeout(function () {
if(i == "going") array = [];
console.log(array[i])
}, 1000 * i);
})(i);
};
And this method will not work as the for loop has already gone through each item and set the timeout already. Therefore when the item matches "going", it wouldn't stop whatever is coming afterwards.
I've seen many other posts about arrays with delay although they all seem to parse all the items in an array and multiplying the delay by the counter(this is not what I want).
I'm wanting a looping function that would allow me to fully stop any looping of values after a certain item. (but also have delay between each value)