0

how could I use javascript make it so that a while loop takes a one - second break in between it's cycles? for example, if I have

var Timer1 = 0;
var Timer2 = 0;
var Timer3 = 0;

While (Timer1 < 257)
{
    Timer1++;
    if (Timer1 = 256)
    {
    Timer2++;
    Timer1 = 0
    }
    if (Timer2 = 256)
    {
    Timer3++;
    Timer2 = 0
    }
    if (Timer3 = 256)
    {
    Timer1 = 257;
    Timer3 = 0;
    console.log("All Done!")
    }
}

How could I make it so that the loop takes a second in between the time when it's done with the if statements and when the while loop runs again?

1 Answers1

0

One options is using setTimeout().

This example allows switching between timers

var timers = [{
  name: "Timer 1",
  count: 0
}, {
  name: "Timer 2",
  count: 0
}, {
  name: "Timer 3",
  count: 0
}];

var maxCount = 5; //change as per your need, 257

var allDone = function() {
  var done = true;
  var i = timers.length;
  while (i--) {
    if (maxCount > timers[i].count) {
      done = false;
      break;
    }
  }
  return done;
};

var delay = 1000; //1 second

var loop = function() {
  var timer = timers.shift(); // pick the first in queue
  if (maxCount > timer.count)
    timer.count++;
  console.log(timer);
  timers.push(timer); //put in the end of queue
  if (!allDone())
    setTimeout(loop, delay);
  else
    console.log('all done ...');
};

setTimeout(loop, delay);
Open console ..