I'm trying to log the change of a value in the console (Firefox/Firefly, mac).
if(count < 1000)
{
count = count+1;
console.log(count);
setTimeout("start开发者_运维百科Progress", 1000);
}
This is only returning the value 1. It stops after that.
Am I doing something wrong or is there something else affecting this?
You don't have a loop. Only a conditional statement. Use while
.
var count = 1;
while( count < 1000 ) {
count = count+1;
console.log(count);
setTimeout("startProgress", 1000); // you really want to do this 1000 times?
}
Better:
var count = 1;
setTimeout(startProgress,1000); // I'm guessing this is where you want this
while( count < 1000 ) {
console.log( count++ );
}
I think you are looking for while
loop there:
var count = 0;
while(count < 1000) {
count++;
console.log(count);
setTimeout("startProgress", 1000);
}
As the other answers suggest, if
vs while
is your issue. However, a better approach to this would be to use setInterval()
, like this:
setinterval(startProcess, 1000);
This doesn't stop at 1000 calls, but I'm assuming you're just doing that for testing purposes at the moment. If you do need to stop doing it, you can use clearInterval()
, like this:
var interval = setinterval(startProcess, 1000);
//later...
clearInterval(interval);
精彩评论