0

So, I have a localhost website where you can press a button to start a while loop. My intent is to loop through this while loop and "wait" for button clicks from the additional buttons, until 135 seconds have passed. Then, it is supposed to exit the loop. When the code (which is embedded in html) is executed, it only gets to printing "test1", while failing to leave the loop and print test2.

        allyTrack = 0;
        scaleTrack = 0;
        enemyTrack = 0;

        allyString = ""
        scaleString = ""
        enemyString = ""

        allyClicked = false;
        scaleClicked = false;
        enemyClicked = false;

        function allyAdd(){
            allyClicked = true;
        }

        function scaleAdd(){
            scaleClicked = true;
        }

        function enemyAdd(){
            enemyClicked = true;
        }

        //Making the long vectors
        function startGame(){
            var startTime = new Date().getTime();
            //var currentTime = new Date().getTime();
            while(true){
                document.getElementById('test').innerHTML = 'test1';
                if(allyClicked){
                    allyTrack += 1;
                    allyString.append(allyTrack.toString())
                    allyClicked = false;
                }
                else if(scaleClicked){
                    scaleTrack += 1;
                    scaleString.append(scaleTrack.toString())
                    scaleClicked = false;
                }
                else if(enemyClicked){
                    enemyTrack += 1;
                    enemyString.append(enemyTrack.toString())
                    enemyClicked = false;
                }
                else{
                    allyString.append(allyTrack.toString())
                    scaleString.append(scaleTrack.toString())
                    enemyString.append(enemyTrack.toString())               
                }
                currentTime = new Date().getTime();

                if(currentTime-startTime > 2000){
                    break;
                }
            }
            document.getElementById('test').innerHTML = 'test2';
            document.getElementById('list').innerHTML = allyString + '\n' + scaleString + '\n' + enemyString;
        }
Riley Fitzpatrick
  • 869
  • 2
  • 16
  • 38
  • The JavaScript engine will only evaluate one section of your code at a time (single-threaded). Functions like `allyAdd()` cannot be called until `startGame()` and its `while` loop have finished. Adding a delay between iterations can allow the engine moments to evaluate other code. – [Create a pause inside a while loop in javascript](https://stackoverflow.com/questions/4548034/create-a-pause-inside-a-while-loop-in-javascript) – Jonathan Lonowski Mar 10 '18 at 18:06
  • As long as the loop is running, there are no click events. They get delayed until after the loop has finished. You are just freezing your page. – Thomas Mar 10 '18 at 18:19
  • @JonathanLonowski So if I create pauses inside will it automatically allow time to respond to button clicks. Or would the button clicks have to line up with the pauses? – Riley Fitzpatrick Mar 10 '18 at 19:25
  • @RileyFitzpatrick The click events will be run in between the next two iterations of the timeout, so the user shouldn't have to necessarily click during the pauses. Each click event and timeout will be added to a queue, which the engine just works through one at a time (event loop). – Jonathan Lonowski Mar 10 '18 at 19:46

2 Answers2

3

String.append() doesn't belong to JavaScript. I replaced it with concat() and now works.

2

You could use the global setTimeout method:

function loop(callback, interval) {
  return setTimeout(function() {
    callback();
  }, +interval);
}

loop(function() {
  console.log("Looping...");
}, 1000);
baeyun
  • 228
  • 1
  • 6