0

I have a loop and upon certain condition I am trying to have a Jquery deferred object to wait for user input before continuing or breaking the loop.

If I place the confirm().then(block before the function confirm(){ it tells me that there are not enough parameters in the confirm function. If I place it after, then the loop is never "paused" waiting for user interaction.

Would appreciate your help.

Thanks

      for(...) {
          if (condition_that_needs_user_interaction){          
              function confirm() {  
                var defer = $.Deferred(); 
                $('<div div="dialogError"></div>').html("Error. Continue?").dialog({
                                            autoOpen: true,
                                            close: function() {  
                                        $(this).dialog('destroy');
                                    },
                                        title: '<span style="color: red";>Warning!</span>',
                                        modal: true,
                                        buttons: {
                                            'No': function() { 
                                                defer.resolve("no"); 
                                               $(this).dialog("close"); 
                                            },
                                            'Yes': function() { 
                                            defer.resolve("yes");
                                                                $(this).dialog("close"); 
                                            },
                                            'Apply to All': function() {                            
                                            defer.resolve("all"); 
                                                $(this).dialog("close"); 
                                            }
                                        }
                                });

                                return defer.promise(); 
                            }


                            confirm().then(
                                function (answer) {
                                    if(answer == "No") return false;
                                    else if(answer == "Yes") {
                                        //do something;
                                    } else if (answer == "all") {
                                        //do something;
                                    }


                                }, function(answer) {

                                }, function(answer) {

                                });
           } else {
        // biz as usual
           }
    } //end for loop    
Gian
  • 549
  • 3
  • 7
  • 18
  • possibly duplicate http://stackoverflow.com/questions/5551378/javascript-pausing-execution-of-function-to-wait-for-user-input – rahul Sep 03 '12 at 05:53
  • Consider re-formatting your question to have only 2 or 4 spaces for indent and fewer empty lines, it's very hard to read using the SO interface. – RobG Sep 03 '12 at 05:56
  • 1
    Your code is structured incorrectly for what you're trying to do. Since Javascript doesn't have any kind of threading, "pauses" don't work. --- Try to adopt an asynchronous style, and the solution will come naturally: http://shinetech.com/thoughts/articles/139-asynchronous-code-design-with-nodejs- (nodejs-specific article, but the material applies equally well for browser-side js) – Deestan Sep 03 '12 at 06:00
  • 2
    You're looking at this from the wrong way. You should be using events instead – GordonM Sep 03 '12 at 06:02
  • when I use a simple Javascript confirm(), it pauses the loop. Why not with a Jquery dialog? – Gian Sep 03 '12 at 06:03
  • @GordonM, can you please point to an example using "events"? thanks – Gian Sep 03 '12 at 06:04
  • confirm() is a blocking method. In the JS world this is quite unusual - alert, prompt and confirm are the regular, blocking methods. And XMLHttpRequest, if set to sync-mode (although async is preferred.) Everything else is async. You can't block in js, apart from what I listed. The solution is to avoid your loop and use events instead. – Onkelborg Sep 03 '12 at 06:14

0 Answers0