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
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

本文标签: