Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript "while" loop without freezing browser?

I'm trying to delay the default action of clicking on a link, until the user clicks "yes" or "no" to confirm. The problem is that the while loop is freezing the browser (and not even displaying the dialog, even though "open" is called first):

$(".remove").click(function() {
  $("#dialog").dialog("open");

  while (1) {
    // Count sheep
  }
});

Obviously, the "1" is only there for testing purposes. Eventually I want it to check a variable that is set by the dialog. For now though, I want to know how I can use a loop like this to delay the default click action.

Thanks,

Michael

like image 824
Michael Avatar asked Mar 25 '11 01:03

Michael


1 Answers

You can't. You have to exit the function for the dialog to show up. The browser will not do any updates at all as long as the function is running.

You have to use events to handle the user input.

like image 53
Guffa Avatar answered Oct 20 '22 00:10

Guffa