Get Websql values when loading the page through Javascript

Asked

Viewed 44 times

0

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

I need that when loading the page get all the values of WEBSQL and make a calculation to be informed on the 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);
		});
	}

1 answer

0

Try doing 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);
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.