2016-01-11 180 views
-1

執行dragTrack()函數後需要運行helloWorld()。但在dragTrack之後未被調用。回調函數jQuery

dragTrack(function(){ 
    helloWorld(); 
}); 

function dragTrack() { 
    alert('first'); 
} 

function helloWorld() { 
    alert('second'); 
} 
+0

dragTrack不接受任何參數? –

+0

你應該先研究,而不是問問題,,,參考這些答案http://stackoverflow.com/questions/18514504/how-to-call-a-function-from-another-function-in-jquery –

回答

7

你傳遞一個函數作爲參數,但dragTrack需要改變以接受回調,並調用它

dragTrack(helloWorld); 
 

 
function dragTrack(callback) { 
 
    alert('first'); 
 
    if (callback) { 
 
    callback(); 
 
    } 
 
} 
 

 
function helloWorld() { 
 
    alert('second'); 
 
}

0

你逝去的helloWorld()作爲參數在你的dragTrack()調用中,但你沒有處理它。你的dragTrack函數需要一個回調參數,所以你可以使用你的helloWorld()函數作爲參數。