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

154
Views
¿Puedo hacer que esta cuenta regresiva funcione en una sola función?

Este código funciona bien (es una cuenta regresiva para un proyecto que estoy haciendo), pero quiero hacerlo con una sola función. ¿Es posible?

 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 answers
Answer question

0

Inicialice el contador en la función principal. Si llega a cero, llame a la otra función; de lo contrario, registre el conteo y vuelva a llamar a la función pasando el conteo reducido.

 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 Report

0

¿Qué pasa con algo como esto:

 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)

Esto también muestra un "Restante" al principio para que el usuario sea notificado con los segundos de inicio reales.

Además, no necesita mantener ninguna referencia a las variables de intervalo o window .

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!