admin管理员组文章数量:1420966
I am creating an app which fetch the data from the database on startup for that i want to create an js which will perform the database transaction and return the array element through which i will create the text-box and button etc.Know the problem is when I call the function on the onload like elementArray=fetchdata(); and then db.transaction in js will fetch the data from the database and return and array but when i call the function in js it call the function and start execution but does not wait for the db.transaction to plete and starts futher execution.I want to wait untill db.transaction plete its operation and then execute further operation. here is the dynamic_form.js file i create to fetch the data from the database:
var elementArray=new Array();
function fetchdata(){
db.transaction(function(tx){
tx.executeSql("Select * from contactTable",[],function(tx,results){
for(var i=0;i<results.rows.length;i++){
var element=new Object();
element.Name=results.rows.item(i).name;
element.Lastname=results.rows.item(i).last_name;
element.Mobile=resulst.rows.item(i).mobile_no;
// ****and so on***//
elementArray[i]=element;
}
return elementArray;
});
});}
here is the code in the html file it loads the js file :
function dbTranscation()
{
console.log("$ function");
var elementArray=fetchdata();
// after this it sould call this function
createfield();
}
I am creating an app which fetch the data from the database on startup for that i want to create an js which will perform the database transaction and return the array element through which i will create the text-box and button etc.Know the problem is when I call the function on the onload like elementArray=fetchdata(); and then db.transaction in js will fetch the data from the database and return and array but when i call the function in js it call the function and start execution but does not wait for the db.transaction to plete and starts futher execution.I want to wait untill db.transaction plete its operation and then execute further operation. here is the dynamic_form.js file i create to fetch the data from the database:
var elementArray=new Array();
function fetchdata(){
db.transaction(function(tx){
tx.executeSql("Select * from contactTable",[],function(tx,results){
for(var i=0;i<results.rows.length;i++){
var element=new Object();
element.Name=results.rows.item(i).name;
element.Lastname=results.rows.item(i).last_name;
element.Mobile=resulst.rows.item(i).mobile_no;
// ****and so on***//
elementArray[i]=element;
}
return elementArray;
});
});}
here is the code in the html file it loads the js file :
function dbTranscation()
{
console.log("$ function");
var elementArray=fetchdata();
// after this it sould call this function
createfield();
}
Share
Improve this question
asked Aug 12, 2013 at 5:38
Neerav ShahNeerav Shah
7435 gold badges19 silver badges46 bronze badges
3
- Can you check the returned element Array, if it contains data, then only call the method createfield() OR add the createfield as a callback funtion with the returned elementArray. – Sheetal Commented Aug 12, 2013 at 5:51
- Thats where I'm stuck how the check the return elementArray can you give a some example of it plz it would be great help.@Sheetal – Neerav Shah Commented Aug 12, 2013 at 5:55
- I tried this but before it returns a value from js files it executes the if condition – Neerav Shah Commented Aug 12, 2013 at 6:03
2 Answers
Reset to default 2With callback:
var elementArray=new Array();
function fetchdata(callback){
db.transaction(function(tx){
tx.executeSql("Select * from contactTable",[],function(tx,results){
for(var i=0;i<results.rows.length;i++){
var element=new Object();
element.Name=results.rows.item(i).name;
element.Lastname=results.rows.item(i).last_name;
element.Mobile=resulst.rows.item(i).mobile_no;
// ****and so on***//
elementArray[i]=element;
}
callback(elementArray);
});
});}
function dbTranscation()
{
console.log("$ function");
fetchdata(createfield);
}
// another function
function createfield(elementArray)
{
// some logic
};
var elementArray=new Array();
function fetchdata(callbackFN){
db.transaction(function(tx){
tx.executeSql("Select * from contactTable",[],function(tx,results){
for(var i=0;i<results.rows.length;i++){
var element=new Object();
element.Name=results.rows.item(i).name;
element.Lastname=results.rows.item(i).last_name;
element.Mobile=resulst.rows.item(i).mobile_no;
// ****and so on***//
elementArray[i]=element;
}
callbackFN();
});
});}
// you can call function with call back
function dbTranscation() {
console.log("$ function");
fetchdata(function (){
createfield();
});
}
// you can define "fetchdata" function in globally and can access from any other pages
本文标签:
版权声明:本文标题:javascript - How to wait for database transaction to complete it operation and return value in phonegap - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745330457a2653787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论