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){}
我需要将insert maxLeadId
调用到insert函数中。
请建议我如何在java Script中编写回调函数。
答案 0 :(得分: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()以获取回调,然后在计算答案时调用它。
如果你认为的话,不要忘记将其标记为正确的答案。