JQuery + If Statement

I am trying to teach myself basic jquery and am having problems with the if statement that I am trying to use. My code is as follows:

var animate = 0;

$('a').click(function () {
if (animate == 0) {
$('#shadow').fadeOut(500);
var animate = 1;
}
});

I hope to use some other expressions further down the line, so that depending on the value of "animate", another jquery action will be executed when clicked. I'm sure I missed something obvious, but I hit my head against the wall, trying to figure out what it is.

Any help would be appreciated!

+3
source share
3 answers

You cannot use the keyword varagain when assigning 1 to animate. By doing this, you are causing a syntax error, as it is animatealready declared in the same scope.

+5

var , , , .

( ) - - :

var animate = 0;
$('a').click(function() {
    if (animate === 0) {
        $('#shadow').fadeOut(500);
        animate = 1;  // note the lack of "var"
    }
});

, , , ; , animate 0 ( fadeOut).

, , ( ) , :

$('a').click(function() {
    if (!$('#shadow').hasClass('animating')) {
        $('#shadow').addClass('animating').fadeOut(500, function() {
            $(this).removeClass('animating');
        });
    }
});

, , .

+6

, var click.

var animate = 1;

to

animate = 1;

, .

+2

All Articles