0

I just try to build a counter, which counts the seconds but it prints the second only once. Actually, the times function runs each second but it does not update the printed value before. Is there any way for it?

var times = () => {
  const d = new Date(); 
  return d.getSeconds();
 }
console.log(setInterval(times, 1000)); // Write the console.log once and update the inner value each second
H T
  • 1
  • 1
  • youll need a loop that runs at your interval and inside the loop you console.log – Dan May 27 '22 at 20:27
  • 1
    ^ This, also you can't update a line of console.log in the browser. Try moving your `console.log` into your `times` function. – Scott May 27 '22 at 20:28
  • 1
    Alternatively, `console.log` inside `times`. Or `setInterval(() => console.log(times()), 1000)` – CollinD May 27 '22 at 20:28
  • I just looking for a way to overwrite a new value on the previous value. – H T May 27 '22 at 20:38
  • For node see: [Node.js console.log - Is it possible to update a line rather than create a new line?](https://stackoverflow.com/questions/17309749/node-js-console-log-is-it-possible-to-update-a-line-rather-than-create-a-new-l). In chrome, you can't update it but you can create live views of variables in the document, see: [Is there a way for console.log() to update a line instead of appending new ones?](https://stackoverflow.com/questions/53564873/is-there-a-way-for-console-log-to-update-a-line-instead-of-appending-new-ones) – pilchard May 27 '22 at 21:33

0 Answers0