Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
Making a clock autorefresh using setInterval

i wanted to build a clock and my only problem here is that setInterval basically does not work. Am I missing something

const timer = document.getElementById("container")
let currentTime = new Date()

let hours = currentTime.getHours()
let minutes = currentTime.getMinutes()
let seconds = currentTime.getSeconds()



function time () {

        let hoursIf = hours.toString().length==1 ? "0"+hours : hours
        let minutesIf = minutes.toString().length==1 ? "0"+ minutes : minutes
        let secondsIf = seconds.toString().length==1 ? "0" +seconds : seconds

        timer.textContent = `${hoursIf}:${minutesIf}:${secondsIf}`

}
time()
let run = setInterval(time, 1000)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It works, trust me. The problem is the assigment in:

let hours = currentTime.getHours()
let minutes = currentTime.getMinutes()
let seconds = currentTime.getSeconds()

You're assigning time once at the start, and don't check for updated values later. If you'd put that in your time function it would work properly. Something like this:

function time () {
        let currentTime = new Date()

        let hours = currentTime.getHours()
        let minutes = currentTime.getMinutes()
        let seconds = currentTime.getSeconds()

        let hoursIf = hours.toString().length==1 ? "0"+hours : hours
        let minutesIf = minutes.toString().length==1 ? "0"+ minutes : minutes
        let secondsIf = seconds.toString().length==1 ? "0" +seconds : seconds

        timer.textContent = `${hoursIf}:${minutesIf}:${secondsIf}`

}
time()
let run = setInterval(time, 1000)
about 4 years ago · Juan Pablo Isaza Relatório

0

You aren't updating your variables. You have them as globals, but never update their values, so the method "time" keeps using the same values.

let currentTime = new Date()

let hours = currentTime.getHours()
let minutes = currentTime.getMinutes()
let seconds = currentTime.getSeconds()

These should be set in your method or at least updated to current values. Otherwise you are simply re-displaying the same time every time executes.

If you put a "Console.log" in your method, I'd bet you'd see that it does run, it just doesn't update the time as you expect.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda