Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
¿Cómo hacer que el temporizador de conteo aumente en horas, minutos y segundos en javascript?

Quiero hacer un temporizador de conteo desde las 00:00:00, el conteo comienza si "div id = data" se llena con "const date" y el tiempo aumenta hasta que el código recibe el gatillo de parada. como puedo lograr eso?

Aquí está mi código actual :

 <div id="data"></div> <p id="demo"></p> <script> const api_url = 'json.php' async function okejson() { const resp = await fetch(api_url); const dat = await resp.json(); const awal = (dat[0]) const date = awal.tanggal document.getElementById("data").innerHtml = date var distance = 0; var x = setInterval(function() { distance +=1; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; }, 1000); } </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El uso setInterval no arrojará resultados precisos. Es aceptable para períodos cortos y aplicaciones no críticas. Si puede llevar horas, debería considerar usar el reloj del sistema. Sin embargo, aquí hay un constructor que puede usar para generar un objeto que tiene un método de inicio (y también de parada y reinicio). El método de inicio acepta una función de devolución de llamada a la que llamará cada segundo y pasa un objeto con propiedades de days , hours , minutes y seconds . Puedes usarlo para hacer lo que quieras.

 function Timer() { this.value = 0 this.updateCb = null this.interval = null function getTime() { console.log(this.value) var seconds = this.value % 60 var minutes = Math.floor(this.value / 60) var hours = Math.floor(this.value / 3600) var days = Math.floor(this.value / (3600 * 24)) return { days: days, hours: hours % 24, minutes: minutes % 60, seconds } } this.start = function (cb) { if (cb) this.updateCb = cb clearInterval(this.interval) var self = this interval = setInterval(function () { self.value += 1 if (self.updateCb) self.updateCb(getTime.bind(self)()) }, 1000) } this.stop = function () { this.clearInterval(interval) } this.reset = function () { this.value = 0 clearInterval(interval) } } var timer = new Timer() timer.start(function (time) { console.log(time) })

Puede iniciar el temporizador al hacer clic en un botón o cualquier otro evento.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!