[英]Is there an alternative to console.log() which lets you print out and update a single line instead of spamming the console?
I am looking for a way to print out a given variable f.ex. 我正在寻找一种打印出给定变量f.ex的方法。 the i of a for-loop for each iteration, without it resulting in the entire console being filled with new lines of new values. 每次迭代的for循环的i,没有它导致整个控制台充满新的新值。 Is there a console. 有控制台吗? method for just printing one line, and updating that as we go? 只打印一行的方法,并在我们去的时候更新它?
I realise that you could do this by implementing a text-field in your program which you change the with each iteration, but if there is a way of doing this in the console it would be a bit easier (and perhaps quicker? although I am really not sure about that). 我意识到你可以通过在你的程序中实现一个文本字段来实现这一点,你可以在每次迭代时更改它,但如果有一种方法可以在控制台中执行此操作,那么它会更容易一些(也许更快一些?虽然我是真的不确定)。 Thanks in advance. 提前致谢。
If there is still confusion about what im asking, what i want is my console to print out: "i = " i once, and then update the i in that one line, instead of: 如果仍然有关于我要求的内容的混淆,我想要的是我的控制台打印出来:“i =”我曾经,然后在那一行更新i,而不是:
i=1
i=2
i=3
1=4
.
.
.
which gets really messy as you go. 你去的时候会变得很乱。 For the exact example of the i in a for loop, you could get this value from just console.log()'ing the same thing for each iteration, and a number will pop up beside it (in firefox anyway), but i would like to be able to do this with some more useful information. 对于for循环中的i的确切示例,您可以从console.log()获取此值,每次迭代都会出现相同的内容,并且会在它旁边弹出一个数字(无论如何都在firefox中),但我会希望能够通过一些更有用的信息来做到这一点。
Option 1 : use console.groupCollapsed()
and console.groupEnd()
: 选项1 :使用console.groupCollapsed()
和console.groupEnd()
:
console.groupCollapsed();
for (let i = 0; i < 100; i+= 1) { console.log(`i = ${i}`) }
console.groupEnd();
Option 2 : set the values in an array or a string and log the var when the iterations finish: 选项2 :在数组或字符串中设置值,并在迭代完成时记录var:
let valuesToLog = [];
for (let i = 0; i < 100; i+= 1) { valuesToLog.push(`i = ${i}`) }
// see it as an array
console.log(valuesToLog);
// see it as a string, with each value separated by ', '
console.log(valuesToLog.join(', '));
how about JQuery console.clear()
? 怎么样的JQuery console.clear()
?
$(function(){ loop_counter(); }); function loop_counter() { for (let i = 1; i <= 100; i++){ console.clear(); console.log ("i=", i) } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.