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

155
Vistas
Can I make this countdown work in only one function?

This code works fine (It is a countdown for a project I'm doing), but i want to do it with only one function. It is possible?

function workingCountdown(){
    //Set an interval that call the countdown function every second
    window.variableCountdown = setInterval(countdown, 1000)
}
function countdown(){
    //CHECK IF SECONDS IS BIGGER THAN ONE
    if(seconds>1){
        //IF YES
        //Substract 1 to the seconds variable
        window.seconds--
        //Change the countdown text according to seconds left
        document.getElementById("countdown").innerHTML = "You have " + seconds + " seconds left"
    } else {
        //IF NO
        //Notify the user that they ran out of time by changing the countdown text
        document.getElementById("countdown").innerHTML = "You have 0 seconds left"
        //Let pass fifty miliseconds so the countdown text can change before the alert is played
        setTimeout(function(){cpsTestFinish()}, 50) 
        //Stop the countdown repeat
        clearInterval(variableCountdown)
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Initialise the counter in the main function. If it reaches zero call the other function, otherwise log the count, and call the function again passing in the reduced count.

const countdown = document.querySelector('#countdown');

function fn() {
  countdown.innerHTML = `You ran out of time!`;
}

function timer(seconds = 5) {
  if (seconds === 0) {
    fn();
  } else {
    countdown.innerHTML = `You have ${seconds} seconds left`;
    setTimeout(timer, 1000, --seconds);
  }
}

timer();
<div id="countdown"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

What about something like this:

function countdown(remaining) {
  if(remaining > 0) {
    // update the remaining time UI
    console.log(`Remaining ${remaining}`);
    setTimeout(() => countdown(remaining - 1), 1000)
  } else {
    // do whatever you need to do when the countdown is over
    console.log("DONE");
  }
}
// then you call
countdown(10)

This also displays a "Remaining" at the very start so the user is notified with the actual starting seconds.

Also you don't need to keep any reference to the interval or window variables.

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