1

How can I count up in jQuery in a for each function by just 0.5 instead of 1?

$(".item").each( function(i) {
  console.log(i+0.5);
});

At the moment this goes up by .5 + 1. Can I only jump up by 0.5 each time? The logic as to why I want to do this is because I'm setting an animation delay for each item within a certain element. So for each item I want to increase the delay via this function.

Gerico
  • 5,079
  • 14
  • 44
  • 85
  • This will help you understanding what is going wrong: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Marc Apr 01 '15 at 20:35
  • 1
    Interesting theoretical question, but in practice, what are you trying to accomplish with this logic? FYI - I didn't downvote, likely reason is your question was too simple or didn't show much effort. – Tim Lewis Apr 01 '15 at 20:39
  • @TimLewis Updated with a little more info. Not sure for the downvote? I thought it was a legit question... – Gerico Apr 01 '15 at 20:41

1 Answers1

0

As per the doc of each, the first argument of the function is an integer representing the index. So, what you want to do actually is

$(".item").each( function(i) {
  console.log(i*0.5);
});
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83