Get values from WebSQL when loading page through Javascript

0

When I load the same page as the getChamadasFromDay () function starting after loading, the variables total_time and total_calls continue to be zero. note that the total_time variable and total_call variable are receiving values but for some reason remain 0 Zero . If you make a console.log(results.row.item(i).d_time) it prints the values in the console.

I need when I load the page get all the values of WEBSQL and make a calculation to be informed on screen.

var total_time 		= 0;
var total_chamadas 	= 0;

window.addEventListener('load',function(){initDB()},false);


function initDB(){
		db = openDatabase (database_name, database_version, database_description, database_size);
		if(!db){
			alert('Erro Navegador nao possui suporte ao WebSQL.')
		}
		else{
			getChamadasFromDay();
		}
	}

function getChamadasFromDay(){
	db.transaction (function (tx) {
	   	tx.executeSql ('SELECT * FROM chamadas WHERE d_day=? AND d_month=? AND d_year=?', [
		   		data.getDate(),
				data.getMonth(),
				data.getFullYear()
		   	], function (tx, results) {
		   		var len = results.rows.length;
		   		for (i = 0; i <len; i ++) {
		   			total_time += parseInt(results.rows.item(i).d_time);
		   			total_chamadas++;
		   		}
		 	}, null);
		});
	}
    
asked by anonymous 10.07.2015 / 07:43

1 answer

0

Try it this way:



function getChamadasFromDay() {

    db.transaction (function (tx) {

         var total_time     = 0;
         var total_chamadas = 0;

         tx.executeSql ('SELECT *
                        FROM  chamadas
                        WHERE d_day=?
                        AND   d_month=?
                        AND   d_year=?',
               [
                data.getDate(),
                data.getMonth() + 1, //em javascript: janeiro é 0
                data.getFullYear()
               ], function (tx, results) {
                     for (var i in results.rows) {
                         total_time    += parseInt(results.rows.item[i].d_time);
                         total_chamadas++;
                     }
                  }, null);
    });
}

    
13.07.2015 / 16:29