-1
        function falling()
        {
            isFalling = true;

            while (isFalling == true)
            {

                if (y < 120) {

                    y++;

                }
                else if (y == 120) {
                    isFalling = false;

                }

            }

        }

I have tried adding setTimeout(function() around the entire loop, around the if statement, around the y++. I don't know what i'm doing wrong. Any time I add any of these the page becomes unresponsive once the falling function is called. I'm well aware that this is probably a duplicate question but the duplicate questions failed ti help. { }, 100)

RomanF
  • 23
  • 5

3 Answers3

1

You would do it like this:

function falling(y) {
    if (y < 120) {
        setTimeout(falling.bind(this, y + 1), 100); // specify your delay.
    }
}

falling(0);

The question has indeed been answered several times, and the answers here are really not that different from this.

Note that I removed the apparently global variable isFalling. If you do need that variable in other code, then you can keep that variable updated as follows:

function falling(y) {
    isFalling = y < 120;
    if (isFalling) {
        setTimeout(falling.bind(this, y + 1), 100); // specify your delay.
    }
}
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
  • While it is not 100% clear from the question, it appears that the intent is the management of a period of time where **isFalling** is **true**. If so, then this answer needs a bit more code. – JonSG Oct 15 '16 at 21:08
  • @JonSG, if that is true, the question would really need to specify this as a constraint. Anyway, I what needs to be added to the code to maintain such global state. – trincot Oct 15 '16 at 21:51
0

I would use window.setInterval() instead, as you want it to repeat until a certain number

working plunkr:

https://plnkr.co/edit/TbpplnIShiaJR7sHDUjP?p=preview

function falling()
    {
        var isFalling = true; 
        var y=0;
        myInterval = window.setInterval(function() {
          y++;
          console.log(y);
          if (y== 120) {
            clearInterval(myInterval);
          }
        }, 100)
    }

falling();
pasquers
  • 772
  • 1
  • 8
  • 24
0

If the intent of calling falling() is to temporarily set the value of a global variable to true (as suggested by your current code), then this should do the trick.

var isFalling = false;

function falling(fallDuration) {
  isFalling = true;
  console.log("now falling");

  setTimeout(function(){
    isFalling = false;
    console.log("landed");
  }, fallDuration || 1000);

}

falling();

I imagine though that you might actually want to fall and then continue doing something else. In that case you probably want to look at a callback or into promises. You might do a simple:

var isFalling = false;

function falling(fallDuration, andThen) {
  isFalling = true;

  console.log("now falling");

  setTimeout(function() {
    isFalling = false;
    console.log("landed");
    andThen();
  }, fallDuration);

}

falling(1000, function(){ console.log("now standing"); });
JonSG
  • 10,542
  • 2
  • 25
  • 36