Adrian Solarczyk
Adrian Solarczyk

Reputation: 247

Updating a specific variable

function winners() {
updating = true;
if (mm == "Best of 3") {
    var wygrany = (s1 == "2")? 'left' : 'right';
    return true;
}
if (mm == "Best of 5") {
    var wygrany = (s1 == "3")? 'left' : 'right';
    return true;
}
if (mm == "Best of 7") {
    var wygrany = (s1 == "4")? 'left' : 'right';
    return true;
}
return false; }

This is the code that should be deciding if my var wygrany = 'left' or 'right'.

function runUpdate() {
if (timeOld == timeNew) return;

if (winners == true) {
        updating = true;
            setTimeout(function(){
                    $('.team.center .name').set('$', '-flipInY +fadeOut');
                    if(wygrany == "left") {
                        $('.team.right').set('$', '+animated +fadeOutDown');
                        $('.team.left').set('$', '+winner_show');
                        $('#ww').set('$', '-hidden +fadeIn');
                        $('.bg_winner').set('$', '-hidden +fadeIn');
                    } else {
                        $('.team.left').set('$', '+animated +fadeOutUp');
                        $('.team.right').set('$', '+winner_show');
                        $('#ww').set('$', '-hidden +fadeIn');
                        $('.bg_winner').set('$', '-hidden +fadeIn');
                    }
            updating = false;
        }, 1000);
}

This is the part responsible for display. Although code is not working, my function winners always returns 'true' and then script stops. It is probably syntax error but i can't find it.

Upvotes: 0

Views: 44

Answers (2)

Adrian Solarczyk
Adrian Solarczyk

Reputation: 247

Actually i needed to redefine my function winners a little bit:

function winners() {
updating = true;
if (mm == "Best of 3") {
    if(s1 == "2" || s2 == "2") {
        wygrany = (s1 == "2")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
}
if (mm == "Best of 5") {
    if(s1 == "3" || s2 == "3") {
        wygrany = (s1 == "3")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
}
if (mm == "Best of 7") {
    if(s1 == "4" || s2 == "4") {
        wygrany = (s1 == "4")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
} }

I declared variable wygrany outside scope. But if i change

if (winners == true)

to

if (winners() == true)

script stops even earlier. I forgot to add winners is variable which i declared earlier and forgot to remove, could that affect the code?

Upvotes: 0

jdmdevdotnet
jdmdevdotnet

Reputation: 1

You have a scope issue. You're declaring var wygrany inside of a function scope, which any scope that doesn't reside inside that one does not have access.

//outside scope
var wygrany = '';

function winners() {
updating = true;

if (mm == "Best of 3") {
    wygrany = (s1 == "2")? 'left' : 'right';
    return true;
}
if (mm == "Best of 5") {
    wygrany = (s1 == "3")? 'left' : 'right';
    return true;
}
if (mm == "Best of 7") {
    wygrany = (s1 == "4")? 'left' : 'right';
    return true;
}
return false; }

And then you can access it anywhere. Also you need to do:

if (winners() == true) {

Not sure if winners is a variable too? Confusing how you have it, but if that's what your intent was, you have to add the () to make the function call.

I don't know why it would "always return true" seems like the function isn't even getting called though since you don't invoke the function winners(). But fix these issues and if you're still having trouble we can see why it's always returning true.

Check out developer tools, you would have seen these errors in console debugger.

Upvotes: 1

Related Questions