While (mouseIsPressed) infinite loop nasty

I was trying to use a while statement in the draw loop.
I was thinking it would stop the draw loop until the mouse was released,
but it just goes into an infinite loop.

let redoLines = true

function draw() {
  while (mouseIsPressed) {  redoLines = true } 
  if (redoLines){ 
     doLines()
     redoLines = false
  }
}

eak, I guess it was to nasty bit of nasty code.
So going have to use the not so nasty,

function mouseReleased() {
     redoLines = false
}

Still, I was hoping my nasty code would have worked.

1 Like

Given the thread responsible to change the value of mouseIsPressed is the same 1 executing your while () “infinite” loop, it means it’ll never get the chance to do so. :grimacing:

3 Likes

It’s not nasty, it’s a totally reasonable idea! And you did successfully stop the draw loop.
If you swap that while out for an if, you are in business.

1 Like