Me gustaría crear un temporizador de cuenta regresiva para mi recurso. Un ejemplo de esto lo tomé de la respuesta clonada de Quasimodo de esta página .
Del código tomé algunos elementos, ya que solo necesito minutos y segundos. Y no necesito una marca de 30 minutos.
El código funciona muy bien, pero a diferencia del autor de la pregunta, necesito que el inicio comience y finalice en 1 minuto de la próxima hora.
Los cambios que hice no condujeron al resultado deseado:
secsRemaining = 3600 - (time.getUTCMinutes()+1)%60 * 60 - time.getUTCSeconds(),y
mins = (Math.floor(secsRemaining / 60)+60),Esto dio un resultado, pero no el que se necesita. Cuando la hora en el reloj se convierte en 00 minutos, el código se convierte en 60 minutos y más de 00 segundos. Necesito por ejemplo a las 14:00:59 el timer tiene los valores 00:01, y a las 14:01:01 el timer tiene los valores 59:59.
Por favor, hágame saber cómo se puede cambiar para lograr el resultado deseado. Tal vez usted tiene un enlace a las soluciones. No pude encontrarlo en Internet.
Código que estoy usando:
var byId = document.getElementById.bind(document); function updateTime() { var time = new Date(), secsRemaining = 3600 - (time.getUTCMinutes()) % 60 * 60 - time.getUTCSeconds(), mins = (Math.floor(secsRemaining / 60)), secs = secsRemaining % 60; byId('min-part').textContent = mins; byId('sec-part').textContent = secs; setTimeout(updateTime, 1000 - (new Date()).getUTCMilliseconds()).toLocaleString(); } updateTime(); <div>Time left before update: <span id="min-part"></span>:<span id="sec-part"></span></div>Si entendí bien tus necesidades, este debería ser el código que necesitas:
var byId = document.getElementById.bind(document); function updateTime() { var time = new Date(), // You need an hour of countdown, so 30 becomes 60 secsRemaining = 3600 - (time.getUTCMinutes()+60)%60 * 60 - time.getUTCSeconds(), // integer division // you want the timer to "end" at minute 1, so add 1 minute to the minutes counter mins = (Math.floor(secsRemaining / 60) + 1) % 60, secs = secsRemaining % 60 ; byId('min-total').textContent = secsRemaining; byId('min-part').textContent = mins; byId('sec-part').textContent = secs; // let's be sophisticated and get a fresh time object // to calculate the next seconds shift of the clock setTimeout( updateTime, 1000 - (new Date()).getUTCMilliseconds() ); } updateTime();Así es como lo haría
const minutes = document.getElementById('minutes') const seconds = document.getElementById('seconds') setInterval(() => { const now = new Date() const nextHours = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 1, 1) const nbMilisec = (nextHours - now) const nbMinutes = parseInt((nbMilisec / 1000 / 60) % 60) const nbSeconds = parseInt((nbMilisec / 1000) % 60) minutes.innerHTML = String(nbMinutes).padStart(2, '0') seconds.innerHTML = String(nbSeconds).padStart(2, '0') }, 1000) <div id="time"> Time left before update : <span id="minutes"></span> : <span id="seconds"></span> </div>