My jQuery script don’t return the number in ascending order.
jQuery
$(document).ready(function() {
$.xpto = function(dom, speed) {
i = 0;
interval = setInterval(function() {
i++;
$(dom).append(i + '<br>');
}, speed);
};
$.xpto('#a', 1000);
$.xpto('#b', 2000);
});
And my HTML:
<div id="a" style="background:blue;float:left;"></div>
<div id="b" style="background:red;float:left;"></div>
Thanks!
You’re missing the
varkeyword beforei = 0andinterval. This causes all instances of function$.xptoto share these variables. Furthermore, theivariable resets to zero each time you call$.xpto.According to your function’s logic, this should happen:
If this is not as expected, mention your wishes, and I will have a look at it.