简单的 javascript 控制台日志 (FireFox)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3049098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:05:08  来源:igfitidea点击:

Simple javascript console log (FireFox)

javascriptfirefox

提问by Kevin Brown

I'm trying to log the change of a value in the console (Firefox/Firefly, mac).

我正在尝试在控制台(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.

这仅返回值 1。在那之后它停止。

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

我做错了什么还是有其他影响?

回答by Ken Redler

You don't have a loop. Only a conditional statement. Use while.

你没有循环。只是一个条件语句。使用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++ );
}

回答by Sarfraz

I think you are looking for whileloop there:

我想你正在while那里寻找循环:

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

回答by Nick Craver

As the other answers suggest, ifvs whileis your issue. However, a better approach to this would be to use setInterval(), like this:

正如其他答案所暗示的那样,ifvswhile是您的问题。但是,更好的方法是使用setInterval(),如下所示:

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:

这不会在 1000 次调用时停止,但我假设您目前只是出于测试目的而这样做。如果您确实需要停止这样做,则可以使用clearInterval(),如下所示:

var interval = setinterval(startProcess, 1000);
//later...
clearInterval(interval);