2012-07-04 53 views
0

我有2个函数。 First包含Jquery-UI对话框,并从Second函数调用。喜欢的东西:函数调用另一个函数javascript

function First() { 
    $('div').dialog({ 
     buttons: { 
      "Ok": function() { /* code */ 
      } 
     } 
    }); 
} 

function Second() { 
    First(); 
    /* rest of this function code is depend upon the "Ok button" 
     function code */ 
} 

现在我的问题是,调用函数First后脚本的执行不会等待dialog's Ok button press。我该怎么做,所以只有在按下Ok button之后,控制才能从功能First返回?

+1

使用OK按钮的回调选项 – Blaster

+0

写OK功能的代码 –

+0

移动'秒'功能'OK按钮first'函数调用后'代码'点击我 – TRR

回答

2

从函数Second中调用First后的部分变成第二个函数(这里叫做SecondOkHandler)。呼叫First一个新的参数(这个回调函数),并在功能上First“OK”称之为:

function First(okCallback) { 
    $('div').dialog({ 
     buttons : { 
      "Ok" : okCallback 
     } 
    }); 
} 

function Second() { 
    First(SecondOkHandler); 
} 

function SecondOkHandler() { 
    /* rest of this function code is depend upon the "Ok button" function code */ 
} 

另见this example

=== UPDATE ===

为了使它更复杂,这里a link to an example更多的回调。

+0

已经添加[示例] (http://jsfiddle.net/HD5V8/1/)。 – scessor

1
function First(waitTillOk) { 
     $('div').dialog({ 
      buttons: { 
       "Ok": function() { 
         /* code */ 
         if(typeof waitTillOk == "function"){ 
         waitTillOk(); 
         } 
       } 
      } 
     }); 
    } 

    function Second() { 
     var waitTillOk = function(){ 
     /* rest of this function code is depend upon the "Ok button" 
      function code */ 
    } 
    First(waitTilOk); 
    } 
相关问题