-2

Possible Duplicate:
Sleep in Javascript
What do I do if I want a JavaScript version of sleep()?

Is it possible to implement a sleep function in Javascript?

I have this code:

var state = true;
$('#changeState').click(function(){
  if (state == true)
    state = false
  else
    state = true
})
while() {
  //do something
  if (state == false){
    sleep();
  }
}

Is it feasable?

Thanks for your help.

Community
  • 1
  • 1
Valeriane
  • 768
  • 2
  • 13
  • 35
  • If you want to execute a certain action if the state becomes true, why not just start this action after you set state to true? There is no need for a sleep() method in Javascript like that ... you should describe better what exactly you want to achieve – devnull69 Nov 16 '12 at 09:13
  • on an unrelated side-note you can just use `$('#changeState').click(function(){state = !state;});` – naveen Nov 16 '12 at 09:14

1 Answers1

3

If you attempt to make the browser "busy wait" it will become unresponsive, and may generate warnings to that effect.

You must allow the normal event processing loop to continue, or your click handler will never be called.

Alnitak
  • 313,276
  • 69
  • 379
  • 466