1

Can any body shed some light on why I am not able to stop and start this loop?

 <input type="button" value="start" onclick="start()">
 <input type="reset" value="stop" onClick="stop()">

var startLottery = false;

while(startLottery){
setInterval(function(){automateLottery()},120);
startLottery;
};

var start = function(){
startLottery = true;    
};

var stop = function(){
startLottery = false;   
};

Thank you

3
  • 1
    You don't need to put the interval in a while. Commented Jan 4, 2013 at 18:36
  • 1
    And, what's the point of the last startLottery in the loop? It'll just evaluate a boolean and do nothing with it. Commented Jan 4, 2013 at 18:38
  • @UllerUller Can you select one of the answers below as correct, so the question will be close? Commented Jan 7, 2013 at 23:25

4 Answers 4

2

You cannot start and stop a loop like that. You need to use setInterval and clearInterval:

JS:

var lotteryInterval;

var start = function(){
    lotteryInterval = setInterval(lotteryFunc, 120);
};

var stop = function(){
    clearInterval(lotteryInterval);
};

var lotteryFunc = function () {
    automateLottery();
};

HTML:

<input type="button" value="start" onclick="start()">
<input type="reset" value="stop" onClick="stop()">

Edit: Working demo.

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

1 Comment

Thank you, going to try this.
2

Problem 1

A while loop will only execute when the operand evaluates to true.

However, you start off with your startLottery variable equal to false.

So the while loop will never execute.

Problem 2

At no point are you actually changing the value of the startLottery variable, so even if the loop did execute, it would never terminate.

4 Comments

This won't really help. The problem is that he is using a looping structure instead of set/clearInterval.
Thanks guys, All I am trying to do is stop and start a loop with my stop and start buttons.
I was hoping that when I press the start button I would change startLottery to true and therefore start the loop??
@UllerUller I believe jbabey is correct. I hope you see WHY what you did is not a proper while loop. This is an important thing to understand. But jbabey has code that will do what you want to do.
0

I believe you're looking for this (assuming you do have an automateLottery function):

var timer;

var start = function(){
    timer = setInterval(automateLottery, 120);
};

var stop = function(){
    clearInterval(timer);
};

start();

1 Comment

Yep that's the one. Thanks bFavaretto
0

If what you provided for code is exactly as I see it, the while loop will never be entered. The page will be rendered (the inputs) and then the variable startLottery will be set to false. Next, the loop will evaluate the expression (which will always return false) and never actually enter the loop.

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.