0

I'm trying make a function that returns the amount of wins a player have had based on a local database. However the values 'p1' and 'p2' seems to reset back to 0 every time they exit out of their loops.

function getAmountOfWins(player) {
var p1 = 0, p2 = 0;
db.transaction(function (tx) {
    tx.executeSql("SELECT * FROM Filter WHERE player1='" + player + "'", [], function (tx, results) {
        for (var i = 0; i < results.rows.length; i++) {
            current = results.rows.item(i);
            p1 = p1 + current.p1wins;
        }
    });
});
db.transaction(function (tx) {
    tx.executeSql("SELECT * FROM Filter WHERE player2='" + player + "'", [], function (tx, results) {
        for (var i = 0; i < results.rows.length; i++) {
            current = results.rows.item(i);
            p2 = p2 + current.p2wins;
        }
    });
});
console.log("Total Wins before return: " + (p1 + p2));
return p1 + p2;
}

In this case p1 and p2 seems to be reset to 0 when they exit out of 'db.transaction'. I'm pretty lost at how I keep the data so I can actually return it.

Anyone has a magic fix that prevents this?

2 Answers 2

2

They don't reset. They were never set. The executeSQL and the transaction methods are Async. Which means you have to do somethin lik this:

function getAmountOfWins(player, callback) {
    // Call function, callback is executed after the query calculations
    getPlayerValues(1, player, function(p1num){
        // Call function, callback is executed after the query calculations
        getPlayerValues(2, player, function(p2num){
            // Call the original callback function
            callback( p1num + p2num );
        });
    });
}

// Get the player values
function getPlayerValues(playernr, player, callback){
    db.transaction(function (tx) {
        tx.executeSql("SELECT * FROM Filter WHERE player" + playernr + "='" + player + "'", [], function (tx, results) {
            var p = 0;
            for (var i = 0; i < results.rows.length; i++) {
                current = results.rows.item(i);
                p = p + current.p2wins;
            }
            callback(p);
        });
    });
}

the call:

// Get the amount of wins, this is an async functionm with the callback
getAmountOfWins(player, function(value){
    console.log("Total Wins before return: " + (value));

});
Sign up to request clarification or add additional context in comments.

4 Comments

Wao thanks! I must have never run into any async functions before using javascript. Thanks a lot for providing me with a commented answer!
However testing this and fiddling a little around I realize I still can't pull the value outside the function due to it also being async. Is there any way at all to somehow pull it out? wins = getAmountOfWins(player); ?
The call I provided is still async, if you want to use the value of the function I wrote, it has to be inside, as you can see the console.log does work. Move you code inside the function, and it will work.
Ahh yea I realized just now I can call it inside it self and still have access to the value as you demonstrated with getPlayerValues. Apologies for the trouble, you've helped a lot. Thanks!
0

I'm going to assume that executeSql is asynchronous, since it accepts a callback function.

The body of the function you're giving it is executed at some point in the future, long after console.log as run. You need to refactor your code so that your console.log (and whatever other logic you want to include) are run by the callbacks passed to executeSql.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.