admin管理员组

文章数量:1292244

I'm not too sure what's going on here and feel I may be missing something that is probably quite obvious, but I have an if else in which both statements are being called. If someone could shed some light on this that would be great. The code below is using asual and should be detecting whether or not a div $('#' + $.address.hash() has already been loaded. However both if and else events are being fired in order. In fact the else statement fires twice...

$('#lmit_back').show(400, function() {
    if($('#' + $.address.hash() ).length == 0) {
        $('#init').load('inithandler.php', { 'address' : $.address.hash() }, function() {
            $('#' + $.address.hash()).fadeIn(1000, function() {
                $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });
            });
        });
    }  
    else {
        alert('hey');
    }
});

Here is all the code..I can't really work out what could cause it to execute twice, unless it has something to do with address.change which im not as familiar with as I would like to be.

if (($.address.hash() == '')) {$.address.hash('home');}

    $('#lmit_back').click(function() {

            $.address.hash('home');

    });


    $.address.change( function() {  



        if (!($.address.hash() == 'home')) 
        {



        var exception = '.' + $('[href="#' + $.address.hash() + '"]').parent().attr('class');


            $('#left_menu_bar li:not(' + exception + ')').hide(300,function() {

                $(exception).show( function() {

                    $('#left_menu_bar').animate({ width:'100%'},400, function() { 

                        $('#lmit_back').show(400, function() {


                                if ($('#' + $.address.hash() ).length === 0)
                                {

                                    $('#init').load('inithandler.php', { 'address' : $.address.hash() } , function() { 

                                        $('#' + $.address.hash()).fadeIn(1000, function() {

                                            $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });

                                        });



                                    });

                                }
                                else
                                {

                                alert('nigs');

                                }

                        });

                    });

                });

            });



        }
        else
        {




            $('#left_menu_bar').animate({ width:'251px'}, function() { 

                $('#left_menu_bar li').show( function() {

                    $('#lmit_back').hide();

                });

            });



            $('#left_menu_bar').css('width','251px');



        }


    }); 

I'm not too sure what's going on here and feel I may be missing something that is probably quite obvious, but I have an if else in which both statements are being called. If someone could shed some light on this that would be great. The code below is using asual and should be detecting whether or not a div $('#' + $.address.hash() has already been loaded. However both if and else events are being fired in order. In fact the else statement fires twice...

$('#lmit_back').show(400, function() {
    if($('#' + $.address.hash() ).length == 0) {
        $('#init').load('inithandler.php', { 'address' : $.address.hash() }, function() {
            $('#' + $.address.hash()).fadeIn(1000, function() {
                $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });
            });
        });
    }  
    else {
        alert('hey');
    }
});

Here is all the code..I can't really work out what could cause it to execute twice, unless it has something to do with address.change which im not as familiar with as I would like to be.

if (($.address.hash() == '')) {$.address.hash('home');}

    $('#lmit_back').click(function() {

            $.address.hash('home');

    });


    $.address.change( function() {  



        if (!($.address.hash() == 'home')) 
        {



        var exception = '.' + $('[href="#' + $.address.hash() + '"]').parent().attr('class');


            $('#left_menu_bar li:not(' + exception + ')').hide(300,function() {

                $(exception).show( function() {

                    $('#left_menu_bar').animate({ width:'100%'},400, function() { 

                        $('#lmit_back').show(400, function() {


                                if ($('#' + $.address.hash() ).length === 0)
                                {

                                    $('#init').load('inithandler.php', { 'address' : $.address.hash() } , function() { 

                                        $('#' + $.address.hash()).fadeIn(1000, function() {

                                            $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });

                                        });



                                    });

                                }
                                else
                                {

                                alert('nigs');

                                }

                        });

                    });

                });

            });



        }
        else
        {




            $('#left_menu_bar').animate({ width:'251px'}, function() { 

                $('#left_menu_bar li').show( function() {

                    $('#lmit_back').hide();

                });

            });



            $('#left_menu_bar').css('width','251px');



        }


    }); 
Share Improve this question edited Sep 18, 2012 at 14:39 casperOne 74.5k19 gold badges189 silver badges260 bronze badges asked Sep 18, 2012 at 2:24 Melbourne2991Melbourne2991 11.8k12 gold badges47 silver badges82 bronze badges 8
  • 1 Try replacing == with === and check? – Praveen Kumar Purushothaman Commented Sep 18, 2012 at 2:26
  • 4 putting your code on jsfiddle helps the people answering your question tremendously. – valentinas Commented Sep 18, 2012 at 2:29
  • 5 I suspect you are executing the code more than once. – Nathan Wall Commented Sep 18, 2012 at 2:31
  • @PraveenKumar Gave it a go, results are the same. :( – Melbourne2991 Commented Sep 18, 2012 at 2:37
  • Hm, when I replace if ($('#' + $.address.hash() ).length === 0) with if (!$('#' + $.address.hash() ).length === 0) the alert statement fires first as expected, then the if statement, but it fires 4 times... – Melbourne2991 Commented Sep 18, 2012 at 2:40
 |  Show 3 more ments

3 Answers 3

Reset to default 9

The problem here is not arising from the code you have pasted. The only way the code could be running multiple times and hitting multiple branches is if it is being executed more than once. Look to the surrounding code for places where this could be called more than once.

I faced similar issue, while debugging when if condition was true it also went in else block. Later I added alert and console.log in if/else block's & realized, else was not actually being executed but while debugging it looked like it was in else.

So, whoever faces same issue verify by adding alert or console.log.

I was seeing the same behavior, when I debugged step by step it worked. It ended up being that I was attaching multiple click events so the code fired n times.

本文标签: JavaScriptJQueryif and else statements both being executedStack Overflow