Antonio Colella
Antonio Colella

Reputation: 35

Loop won't stop at given value

I was trying to create a sliding panel that will hide itself with a simple js animation. Here the developing link: http://carportal.webpronto.it just click on "hide panel" to see the search tab go left.

The issue is the following: I tried to give a value for the panel to stop when it reaches the position left: -25% But the panel continue his fool run versus the infinite. here the script i used:

<script type="text/javascript">
    var scrollSpeed = 50;
    var step = 1;
    var current = 0;
    var endposition = 25;   
    function closeBAR(){
        current -= step;  
        if (current == endposition){


            stop;

        }



        $('#colleft').css("left",current+"%"); 



    var init = setInterval("closeBAR()", scrollSpeed);

}
</script>

What's wrong with my script?

Upvotes: 0

Views: 100

Answers (3)

matthias
matthias

Reputation: 2264

Having a "dry" look at your code there seams to be at least the following problem: Your global var current will never reach the value of endposition since it's initialized with a value of -1 at the beginning: current -= step == -1 (0 - 1).

Upvotes: 0

Michal Klouda
Michal Klouda

Reputation: 14521

Replace your line

stop;

with

clearInterval(init);

Also the endposition should be negative:

var endposition = -25;

Consider using jQuery UI effects like Slide instead, it will make your code more readable.

Upvotes: 1

logical Chimp
logical Chimp

Reputation: 1014

you're decrementing current, so, since it starts at 0, it ends up going into negative figures. -25 is not the same as 25, so your 'stop' test fails

Upvotes: 1

Related Questions