0

I want to stop setInterval() when left margin is 1200px. My code is :-

<html>
<body>
<script src="jquery-1.11.0.min.js"></script>
<script>
    $(document).ready(function () {
        $('#btn').click(function () {
            var i = setInterval(function () {
                $('img').animate({
                    'margin-left': '+=100px'
                }, 'fast');
                var a = $('img').css('margin-left');
                if (a == "1200px") {
                      clearInterval(i);
                }
            }, 50);
        });
    });
</script>
<img src="48_800[1].jpg" height="50px" width="50px"><br>
<input type="button" value="start" id="btn"><br>
</body>
</html>

It is not working.Can anyone help me?

Khez
  • 10,172
  • 2
  • 31
  • 51
user3298056
  • 7
  • 1
  • 6

5 Answers5

3

You should cast to an integer the pixels value a = parseInt(a);. (as before you were obtaining values of margin-left with decimals, such as 99.45px and then 199.45px so it was jumping the 100px)

Live example

var i = setInterval(function () {

    $('img').animate({
        'margin-left': '+=100px'
    }, 'fast');
    var a = $('img').css('margin-left');
    a = parseInt(a);
    if (a >= 100) {
        clearInterval(i);
    }
}, 50);


Update


I've just noticed the animation still running after the interval has been cleared. Not sure why is this happening but I found a way to solve the problem by caching the final margin in a variable rather than calculating it inside the animation.

Live example 2

Note that in the examples I'm using >=100 to see the results.

Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • I'm not sure that example is actually living? :) – Liam Feb 21 '14 at 10:39
  • Sorry, you misunderstand. I'm just being [facetious](http://dictionary.reference.com/browse/facetious). You would normally say "Live example". A living example imply's it has a life of it's own. +1 BTW! – Liam Feb 21 '14 at 10:41
  • @cesarandavisa you are right. With quotes is accepted. Thanks. – Alvaro Feb 21 '14 at 10:46
1

Use this instead: Updated

$(document).ready(function () {
    $('#btn').click(function () {
        var i = setInterval(function () {
            $('img').animate({
                'margin-left': '+=100px'
            }, 'fast');
            var a = $('img').css('margin-left');
            //console.log(a.substring(0,a.lastIndexOf('px')));
            if (a.substring(0,a.lastIndexOf('px')) >= 1200) {
                  clearInterval(i);
            }
        }, 50);
    });
});
Java_User
  • 1,303
  • 3
  • 27
  • 38
0

try this:

if(parseInt(a) >= 1200)
{
     clearInterval(i);
}
},50);
Rickert
  • 1,677
  • 1
  • 16
  • 23
0
$(document).ready(function () {
    $('#btn').click(function () {
        var a="";
        while(a != "1200px"){
          $('img').animate({
                'margin-left': '+=100px'
            }, 'fast');
            a = $('img').css('margin-left');
      }
    });
});

your existing code looks erroneous ,besides referring to SetInterval is not required, the same could be achieved with decent while clause itself.

happy Coding :)

dreamweiver
  • 6,002
  • 2
  • 24
  • 39
0

try to change the value for the time into the setInterval method

the number value must to be in milliseconds

cesar andavisa
  • 340
  • 1
  • 11