I wrote simple JQuery function to count from 0 to specified number in a range of divs with certain class name:
<div class="number">50%</div>
<div class="number">75%</div>
this is JQuery code:
$(document).ready(function () {
$(".number").each( function() {
var percent = $(this).text().replace("%","");
console.log("test " + percent);
function countToPercent(percent) {
var interval = setInterval(counter,25);
var n = 0;
function counter() {
if (n >= percent) {
clearInterval(interval);
}
else {
n += 1;
// console.log(n);
$(this).text(n + "%");
}
}
}
countToPercent(percent);
})
});
unfortunetly it isn't working now - there is probably a problem within this is line:
$(this).text(n + "%")
i debbuged it using console and commented console.log(n) works - it counts from 0 to specified number. so it has to be problem with selecting correct DOM element.
is it forbidden to use setInterval() with .each() method or with "this" keyword? or is there some bug in the code?