tags:

views:

246

answers:

3

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?

+4  A: 

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++ );
}
Ken Redler
Thanks for the quick response, Ken!
Kevin Brown
That did the trick, but the timeout isn't working...any idea?
Kevin Brown
I'm guessing you want to fire that off before the loop? Not sure what you're trying to accomplish, as I don't know what startProgress is supposed to be. I assume you mean that to be a function call?
Ken Redler
As SLaks said, you *really* shouldn't be passing a string to `setTimeout`.
Matt Ball
Yes, it's a function call.
Kevin Brown
The call should be just be this, no strings: `setTimeout(startProgress,1000);`
Nick Craver
Yup, made it not a string in the better example.
Ken Redler
Thanks, Ken! This is zooming to 100, any reason that the timeout wouldn't work?
Kevin Brown
The timeout is "pausing" before the startProgress is called, right? I need a second pause between each increment of "count"...
Kevin Brown
Well, you're not doing anything inside the `while` loop other than incrementing and logging. You might take a look at @Nick's answer, where he guesses you may really be looking for `setInterval`. That will run a function repeatedly until you call `clearInterval`.
Ken Redler
+1  A: 

I think you are looking for while loop there:

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

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);
Nick Craver
I'm not sure what he's trying to do, but +1.
Ken Redler