Reputation:
I have this counter with setInterval which stops when more than a certain number .
The problem is that does not stop ?
Any ideas ?
Thank you
jQuery( document ).ready(function() {
var myVar = setInterval(crecer, 1);
function crecer(){
var y = 0;
console.log(y);
y++;
if (parseInt(y) > 100){
myStopFunction();
}
}
function myStopFunction() {
clearInterval(myVar);
console.log("entro");
}
});
Upvotes: 2
Views: 194
Reputation: 832
Each time you are calling your 'crecer' function after interval of 1ms, you are working in a new scope. Javascript has functional scoping for 'var' declaration. Try declaring 'y' outside function 'crecer'.
Upvotes: 0
Reputation: 6561
On each interval you're setting y
to 0 again
Move y to be outside of the function block
var y = 0;
function crecer(){
console.log(y);
y++;
if (parseInt(y) > 100){
myStopFunction();
}
}
Upvotes: 5