Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

SetInterval problem

Hello guys i have this function to make a percent text go from 0% to 65%(example), but this not working on varius elements just in one

$.fn.textPorcent = function() {
  var $percent = $(this);
  curr = parseInt($(this).text());
  to = $(this).data("porcentaje");
  counter = setInterval(function() {  
          if(curr <= to)
          {
              $percent.text((curr++)+'%');
           }
      }, 10);
};
$("#span1").textPorcent();
$("#span2").textPorcent();
$("#span3").textPorcent();
$("#span4").textPorcent();
<span id="span1" data-porcentaje="95">0%</span>
<span id="span2" data-porcentaje="95">0%</span>
<span id="span3" data-porcentaje="75">0%</span>
<span id="span4" data-porcentaje="50">0%</span>

but im getting this numbers 50, 49, 46, 48 i dont know why? what's wrong with my function?

1 Answer

Your element IDs are all the same (span1).

Sorry my bad but that's not the reason of my problem :) i think is something with the setInterval but dunno