2013-04-17 67 views
0
i have written code to get max data from database and then insert that data in another table but it is not giving data first time.it is giving leadid as undefined because it is not waiting result of getMaxLeadId and executing second statement.my code is given below 

var leadId = getMaxLeadId(); 

alert("leadId"+leadId); 

function getMaxLeadId() {} 

同步調用,從數據庫中獲取數據

function getMaxIdData_success(tx, result){} 

後,我需要插入maxLeadId進入插入函數調用。

請建議我一些想法如何在java腳本中編寫回調函數。

回答

3

執行此操作的常用方法是使用稱爲CPS的編程風格的回調。

這樣做的一般形式是

var doSomeCalc = function(resultFunc) { 
    //do some long running calc 
    var result = 4; 

    resultFunc(result); 
}; 

var writeOutResult = function(result) { 
    console.log(result); 
}; 

doSomeCalc(writeOutResult); 

所以你想寫你getMaxLeadId()以這種形式把回調時,它已經計算出答案則應該叫。

如果您認爲這是正確的答案,請不要忘記標記爲正確答案。