How to make javascript variable global in phoneGap api

前端 未结 1 1426
天涯浪人
天涯浪人 2021-01-29 12:06

I am learning how to use phonegap now and learning is going great but I\'m stuck with global variable in phoneGap Api. Actually First I\'m getting the result from xml file and t

1条回答
  •  失恋的感觉
    2021-01-29 12:46

    You don't even need it to be global for this. Define the lastId in the function but before the foreach. If that doesn't work, then making it global should really work, by moving the var lastId; line before function populateDB()

    function populateDB(tx) {
            tx.executeSql('DROP TABLE IF EXISTS fruit');
            tx.executeSql('DROP TABLE IF EXISTS fruit_benefit');
            tx.executeSql('CREATE TABLE IF NOT EXISTS fruit (id INTEGER NOT NULL PRIMARY KEY, fname,fsname,fruit_icon,fruit_image)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS fruit_benefit (id INTEGER NOT NULL PRIMARY KEY, benefit, fruit_id)');
            $.ajax({
            type: "GET",
            url: "data.xml",
            dataType: "xml",
            success: function(xml) {
    
                var lastId;
    
                $(xml).find('fruit').each(function(){
                    var fname = $(this).find('fname').text();
                    var fsname = $(this).find('fsname').text();
                    var fruit_icon = $(this).find('fruit_icon').text();
                    var fruit_image = $(this).find('fruit_image').text();
                    //$('').html(''+fname+'').appendTo('#lbUsers');
                    db.transaction(function(transaction) { 
                        transaction.executeSql('INSERT INTO fruit (fname,fsname,fruit_icon,fruit_image) VALUES (?,?,?,?)',[fname, fsname, fruit_icon, fruit_image],function(transaction, results){
                   lastId = results.insertId; 
                },nullHandler,errorHandler);
                  });
    
                  alert(lastId);
    
                });
            }
        });
    
    }
    

    0 讨论(0)
提交回复
热议问题