2016-05-18 6 views
0
function make(callback) { 
    //some other manipulation to get data to pass it into $.post() 
    $.post(data, function(response) { 
     // do something 
     callback() 
    }); 
} 

function two() { 
    make(function() { 
     console.log('hello'); 
    }); 
} 

console.log('hello')コールバックを使用しましたが、引き続き最初にトリガーされます。どのようにmake()を実行してすべてが終了したら、console.log('hello')をトリガーするか?javascriptコールバック関数

+1

を上記の擬似コードの上には何も、 'にconsole.log(「こんにちは」)'最後に実行すべきではありません。あなたは何か他のことをしているに違いない。 – Satpal

答えて

0

two()に電話をかけるとすぐにコードが機能するはずです。

これは、より良いあなたの状況を可視化することがあります

function requestSomething(callback) { 
    $.post(data, function(response) { 
     // do something 
     callback(); 
    }); 
} 

function callBackFunction() { 
    console.log('done!'); 
} 

// Pass callBackFunction, which gets called after request. 
requestSomething(callBackFunction); 
関連する問題