Kevin Brown
Kevin Brown

Reputation: 12650

Simple javascript console log (FireFox)

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("startProgress", 1000);
 }

This is only returning the value 1. It stops after that.

Am I doing something wrong or is there something else affecting this?

Upvotes: 6

Views: 46686

Answers (3)

Ken Redler
Ken Redler

Reputation: 23943

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++ );
}

Upvotes: 10

Nick Craver
Nick Craver

Reputation: 630339

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);

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382608

I think you are looking for while loop there:

var count = 0;
while(count < 1000) {
  count++;
  console.log(count);
  setTimeout("startProgress", 1000);
}

Upvotes: 1

Related Questions