Can't make the content of the timer update every second

brad_fresh :

In this code, I create a div element and append it to 'timer' div. I would like to primarily clear the div element to display seconds value every second. However, I get seconds only ONE time. I don't understand how to fix it. I added a check to get rid of child elements of timer(if they exist which of course will happen when many elements will be created). How to make the content of the timer div update every second?

let timer = document.getElementById('timer');
let time = setInterval(() => {
  while (timer.firstChild) {
    timer.removeChild(timer.firstChild);
  }
  timer.append(document.createElement('div').textContent = `${new Date().getSeconds()}`)
}, 1000);   
Cameron Little :

Your code appears to be working, try this snippet.

let timer = document.getElementById('timer');

let time = setInterval(() => {
  while (timer.firstChild) {
    timer.removeChild(timer.firstChild);
  }

  timer.append(
    document.createElement('div').textContent = `
        ${new Date().getSeconds()}                
  `)
}, 1000);
<div id="timer"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to put a timer on a JLabel to update itself every second

How do I make a Timer and increment a variable for every second that goes by

Why doesn't my timer print every second that passes?

Can i call a action in every 20 second inside a timer which is always running in every second

UIButton text content keeps resetting every second update

Timer not firing every second on WatchKit

Update UITableView every second

Update time every second

how I can update a string list with a feed of string every second?

How can I let update the time every second?

How can I make the image change every n second

How can i make this simple for loop run every second?

I can' t make the second fieldset appear

How can I have a live timer that updates every second to see for how much my programming is running? python

Vibration on every second using timer in Android

Run timer exactly every second with systemd

JavaScript timer does not count every second

Update TextView every second in Android

Python Tkinter, Update every second

Update React component every second

update TextView every second with ScheduledThreadPoolExecutor

How To Update Canvas Every Second?

How to update NumericProperty every second?

Update every second to display time

Countdown value update every second

Can't update the content of angularjs directive

In React JS I'm trying to create a countdown timer but can't get the first second to be a full second

How can I make this method update the quantity on every call

countdown timer using php, can't make it work