views:

29

answers:

3
count = 0;
total = 2;
jQuery("#slide").everyTime(5000,function(i){
    if(count == total-1) {
        count = 0;
        jQuery(this).stop().animate({backgroundPosition: "0px 0"}, {duration:1000});
    }
    else{
        jQuery(this).stop().animate({backgroundPosition: "-"+950*count+"px 0"}, {duration:1000});
        count++;
    }
});

Hi all, i am trying to work on this. there are some problem with the "950*count". When ever i put an operator into this, it wont' work, but if i remove the *count, it work just fine.

Can someone point out what the problem is?

Thank you

+1  A: 

Put parentheses around the calculation:

"-" + (950 * count) + "px 0"

Otherwise the expression is evaluated from left to right, first concatenating "-" with "950", then trying to multiply that.

Guffa
That's what I just did and it won't work.
DucDigital
@DucDigital - Are you sure you have your counts right? `count` is 0 to start with an increases *after* the first animation, which doesn't do anything since it's still 0 at that point.
Nick Craver
@DucDigital: Can you specify "won't work"? What happens? Do you get any error message?
Guffa
theres is no error message in firebug. can you explain a bit about the 0? if it's 0 supposedly it will show up 0, right?
DucDigital
@DucDigital: Perhaps the browser has a problem with `-0px`, putting the sign in the calculation instead takes care of that, test if that helps: `(-950 * count) + "px 0"`.
Guffa
That's resolve the problem, thanks guffa
DucDigital
+1  A: 

Because there's no such number as -0.

jason
A: 

Did you try changing "-"+950*count to "-"+parseInt(950*count) ?

KMW
It doesn't really make sense to parse a number. That will just turn it onto a string and then back to a number.
Guffa