Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
converting JavaScript countdown seconds in hours/mins/secs

I have to change the following code so that function createTimer gets two parameters (hours and minutes) and the output format has to be "hours minutes seconds" and i am struggling to solve this.

function createTimer(options) {
  var timer,
    instance = this,
    seconds = options.seconds,
    updateStatus = options.onUpdateStatus || function () {},
    counterEnd = options.onCounterEnd || function () {};

  function decrementCounter() {
    updateStatus(seconds);
    if (seconds === 0) {
      counterEnd();
      instance.stop();
    }
    seconds--;
  }

  this.start = function () {
    clearInterval(timer);
    timer = 0;
    seconds = options.seconds;
    timer = setInterval(decrementCounter, 1000);
  };

  this.stop = function () {
    clearInterval(timer);
  };
}

function startTimer() {
  var timer = new createTimer({
    seconds: 10,
    onUpdateStatus: function (seconds) {
      document.getElementById("timer").innerHTML = seconds + " seconds";
    },
    onCounterEnd: function () {
      document.getElementById("timer").innerHTML = "Time's up";
    },
  });
  timer.start();
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to keep decreasing seconds, and use several if conditions to subtract one from minutes and hours.

function createTimer(hours, minutes) {
  var timer,
    instance = this,
    seconds = 0,
    hours = hours,
    minutes = minutes,
    updateStatus = onUpdateStatus || function() {},
    counterEnd = onCounterEnd || function() {};

  function onUpdateStatus(h, m, seconds) {
    document.getElementById("timer").innerHTML = h + " hours " + m + " minutes " + seconds + " seconds";
  }

  function onCounterEnd() {
    document.getElementById("timer").innerHTML = "Time's up";
  }

  function decrementCounter() {
    updateStatus(hours, minutes, seconds);
    if (hours === 0 && minutes === 0 && seconds === 0) {
      counterEnd();
      instance.stop();
    }
    seconds--;
    if (seconds < 0) {
      seconds = 59;
      minutes--;
      if (minutes < 0) {
        hours--;
        minutes = 59;
      }
    }

  }

  this.start = function() {
    clearInterval(timer);
    timer = 0;
    seconds = seconds;
    timer = setInterval(decrementCounter, 1000);
  };

  this.stop = function() {
    clearInterval(timer);
  };
}

function startTimer() {
  var timer = new createTimer(0,1);
  timer.start();
}

startTimer()
<div id="timer"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda