2016-01-11 183 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()函数作为参数。