views:

74

answers:

1

Hello here is my code : var one = 0; var two = 0; var three = 0; var free = 1; function open() {

    if ($('#one').is(':visible')) {
        one = 1;
    }
    else {
        one = 0;
        free = 1;
    }

    if ($('#two').is(':visible')) {
        two = 1;
    }
    else {
        two = 0;
        if (one == 1) {
            free = 2;
        }
    }

    if ($('#three').is(':visible')) {
        three = 1;
    }

    else {
        three = 0;
        if (one == 1 && two == 1) {
            free = 3;
        }

    }
}

and then in the HTML part

<body onload="javascript:window.setInterval('open()', 1000)">

Now the layers one two and three are hidden by default. Now the problem is, at first the HTML output ( in Layer 7) is 1 (value of free) . But after one second it changes to 2. Shouldn't it remain the same? This is as all the layers have the same visibility(hidden) at each point in time...

A: 

give a class ('layers' for example) to each layer in order to collect them all together. Then loop them to find the first non visible and get the id.

var free = undefined;

$('.layers').each(function()
{
    if(!$(this).is(':visible'))
    {
        free = $(this).attr('id');
        break;
    }
});

if(free != undefined)
    alert('layer '+free+' is free');
else
    alert('there are no free layers');
Dalen
Your answer seems fine.But sir, I intend to know what is wrong with my logic Thanks for your reply
Anant
it is not scalable, what if you got 15 layers?
Dalen
Well,what i intend to achieve is show the *smallest* (in terms on layer number) layer which is hidden. I don't see anything logically wrong with my code
Anant
have you tried with some alerts here and there?
Dalen
yes,but the statement if ($('#three').is(':visible')) { three = 1; } else { three = 0; if (one == 1 }doesn't seem to be working.Even though,layer "three" is hidden,the three=1; as per an alert
Anant
without the HTML code it is hard to figure out the problem.
Dalen
the main problem is that the if() statements aren't working. can i post the code somewhere ?
Anant
just post it here if it is not that heavy
Dalen
<style type="text/css"><!--#one { visibility:visible; position:absolute; width:178px; height:308px; z-index:15; left: 744px; top: 117px;}#two { visibility:hidden; position:absolute; width:184px; height:313px; z-index:16; left: 453px; top: 117px;}#three {visibility:hidden; position:absolute; width:216px; height:333px; z-index:18; left: 205px; top: 126px;}--></style><div id="three"></div><div id="one"></div><div id="two"></div>
Anant
try to use display:none instead of visibility:hidden
Dalen
for me it works in this way: put your function into a <script> tag. after your function, into the same tag, put this line: $(document).ready(function(){setInterval('open()', 1000);}); this way alert(free) alway show 1 to me
Dalen