title: Javascript - Wait a function to finish
body: <p>I need to implement a confirm box replacement by using jquery dialog. I have a calling function like this </p>
  
  <pre><code>function callingFunc() {
  
   var a = confirmJquery("text", 300, 300, "ok", "cancel"); 
  
   if (a == true) {
     .....
   }
   else {
     .... 
   }
  }
  </code></pre>
  
  <p>This is the confirmJquery function</p>
  
  <pre><code>function confirmJquery(msg, width, height, txtOk, txtCancel) {
  
      var div = document.createElement('div');
      div.className = "confirmJquery";
  
      var span = document.createElement('span');
      $(span).html(msg);
      div.appendChild(span);
  
      var buttonOk = document.createElement('button');
      buttonOk.className = 'buttonStyleBigger';
      $(buttonOk).html(txtOk);
  
      var buttonCancel = document.createElement('button');
      buttonCancel.className = 'buttonStyleBigger';
      $(buttonCancel).html(txtCancel);
  
      var divBottom = document.createElement('div');
      divBottom.className = 'dialogAction';
  
      divBottom.appendChild(buttonOk);
      divBottom.appendChild(buttonCancel);
  
      div.appendChild(divBottom);
  
      var dialog = window.parent.$(div).appendTo(window.parent.document.body);
  
       // open the dialog
      dialog.dialog({
          height: height,
          width: width,
          resizable: false,
          // add a close listener to prevent adding multiple divs to the document
          close: function(event, ui) {
              // remove div with all data and events
              dialog.remove();
          },
          modal: true
      });
  
      $(buttonOk).bind('click', function(){
          return true;
  
      });
  
      $(buttonCancel).bind('click', function() {
  
          return false;
      });
  
  }
  </code></pre>
  
  <p>The problem is, the confirmJquery function always finish before the button (Ok or Cancel) is pressed; hence, there is no value in the calling function. I need to make the confirmJquery waits until user press the button and then function finish and the rest of the calling function continues. How can i do that ? </p>
  
  <p>I need  to update more details: I already tried the call back function way. It works perfectly. But, life is not easy like that. This is a very big, old, messy system. Doing that requires me to re-write lot lot of functions, so i need to create a function that act exactly like the confirm function of javascript </p>