0

I was wondering is it possible to break a while loop with a timer? looked on the internet but could not find a solution for it.

while (true) {
  alert('hi');
} if (timer < 0) {
 timer?
 document.write('Time is up!');
 break;
}

Thank you.

4
  • 5
    best way to implement this is javascript setTimeout Commented Nov 22, 2015 at 17:10
  • 1
    Your example is too broad, as of that giving an answer is not easy. You should change your example code, so that it will show why you need the while(true) loop. Do you only need it to pause it execution until the time is up or do you want to do anything within the loop until the time is up? Commented Nov 22, 2015 at 17:15
  • 1
    Since JavaScript is single-threaded, as long as you're stuck inside the loop nothing else can be evaluated, including something in a setTimeout. The timed callback will be queued and evaluated only when the currently running code has completed. There is probably a solution (not involving a loop) to your actual problem, but not to your silly example. Commented Nov 22, 2015 at 17:32
  • Thank you for the fast response. I was wondering if I could break a loop by using a timer. I am constructing a qwiz a list of questions and I want to add a timer so that if the player is in the game and takes to long a timer will show that he losses and can try again Commented Nov 22, 2015 at 18:39

3 Answers 3

1

You should use setTimeout for this.

var timer = 3;
setTimeout(excuteMethod, 1000);

function excuteMethod() {
  alert(timer + ' call');
  timer--;
  if (timer >= 0) setTimeout(excuteMethod, 1000);
}

Demo : http://jsfiddle.net/kishoresahas/9s9z7adt/

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if this is the correct approach, but it works,

(function() {
  var delay = 30;
  var date = new Date();
  var timer = date.setTime(date.getTime() + delay);
  var count = 0;

  function validate() {
    var now = new Date();
    if (+now > timer)
      return false;
    else
      return true;
  }

  while (true) {
    count++;
    console.log(count);
    if (!validate()) {
      console.log("Time expired");
      break;
    }

    // Fail safe.
    if (count > 50000) {
      console.log("Count breached")
      break;
    }
  }
})()

Comments

0

You can change control value in timer function and break the loop.

var control = true;
while(control)
{
    ...
}

setTimeout(function(){
    control = false;
}, delay); //delay is miliseconds

Or based on counter

var control = true,
    counter = 10;

while(control){
    ...
}

// you can handle as count down
// count down counter every 1000 miliseconds
// after 10(counter start value) seconds
// change control value to false to break while loop
// and clear interval
var counterInterval = setInterval(function(){
    counter--;
    if(counter == 0)
    {
        control = false;
        clearInterval(counterInterval);
    }
},1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.