samedi 2 avril 2016

Issue with If/Elseif Statement in for loop when using cookies (javascript)

Hi in my javascript game i use cookies to load some data when the game starts

function loadData(){
cookieArray = document.cookie.split(';');
for (var i = 0; i < cookieArray.length; i++){
    cookie = cookieArray[i].split('=')[0];
    value = cookieArray[i].split('=')[1];


    if (cookie=="currentR"){

        round = value;

    }
    else if(cookie=="blueWins"){
        bWins=value

    }
    else if(cookie == "redWins"){
        rWins=value

    }

   }
}

When this is called the for loop works fine but the if statement is doing nothing? I tried putting a console.log(cookie) right after the cookie = cookieArray line to test what cookies are being found, which are all of them including; currentR, blueWins, and redWins. but i tried putting a console.log inside each if statement but it doesn't log anything because the if statement never turns true. Why is this, because cookie does equal currentR (and the other 2) at some stage in the for loop. the console isn't logging any errors so i have no idea. and if it helps i'll include the function that saves the data then reloads the page

function saveData(){
clearInterval(reloadG)
document.cookie = 'currentR=' + round
window.alert("Saved Data")
document.cookie = 'blueWins='   + bWins
document.cookie = 'redWins=' + rWins
location.reload(true)
}

I've done some testing and i'm pretty sure the saveData function works fine. Thanks

Aucun commentaire:

Enregistrer un commentaire