javascript - How to wait for database transaction to complete it operation and return value in phonegap - Stack Overflow

I am creating an app which fetch the data from the database on startup for that i want to create an js

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
Add a ment  | 

2 Answers 2

Reset to default 2
With 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330458a4622853.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信