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

153
Views
Repita el temporizador una cantidad infinita de veces

Estoy tratando de agregar un temporizador de cuenta regresiva a mi pago de Shopify usando Google Optimize. Conseguí que esto funcionara usando los siguientes HMTL y JS. Tomado de aquí

Sin embargo, una vez que finaliza el temporizador y vuelvo a cargar la página, comienza desde 17 segundos en lugar de 5 minutos.

¿Hay alguna manera de hacer que esto repita el temporizador de 5 minutos una vez que llegue a 0?

 document.getElementById('timer').innerHTML = 05 + ":" + 00; startTimer(); function startTimer() { var presentTime = document.getElementById('timer').innerHTML; var timeArray = presentTime.split(/[:]+/); var m = timeArray[0]; var s = checkSecond((timeArray[1] - 1)); if(s==59){m=m-1} if(m<0){ return } document.getElementById('timer').innerHTML = m + ":" + s; console.log(m) setTimeout(startTimer, 1000); } function checkSecond(sec) { if (sec < 10 && sec >= 0) {sec = "0" + sec}; // add zero in front of numbers < 10 if (sec < 0) {sec = "59"}; return sec; }
 <div>Your cart is reserved for <span id="timer"></span></div>

Si pudiera dar la respuesta como si estuviera hablando con un principiante completo, sería muy apreciado.

¡Parece que tengo mucho que aprender!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Bienvenido a StackOverflow. Esto debería ayudar...

 //JAVASCRIPT startTimer(5); // SPECIFY AMOUNT OF MINUTES OR NO PARAMETER FOR DEFAULT 5 function startTimer(minutes = 5){ var timeout = minutes * 60000; var ms = timeout; var interval = setInterval(function(){ ms -= 1000; if(ms >= 0) { var d = new Date(1000*Math.round(ms/1000)); document.getElementById('timer').innerHTML = getMS(d); } else { startTimer(5); // MAKE TIMER RESTART AGAIN FOR 5 MINS // clearInterval(interval); // TO MAKE TIMER STOP UPON REACHING 0:00 } }, 1000); } function getMS(d){ return pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()); } function pad(i) { return ('0'+i).slice(-2); }
 <!--HTML --> <body> <div>Your cart is reserved for <span id="timer"></span></div> </body>
about 4 years ago · Juan Pablo Isaza Report

0

En javascript, la función setInterval le permite hacer lo que quiera cada ms fijo.

 interval = setInterval(() => { console.log('Waited for 5m'); // Do whatever you want console.log('Waiting for 5m again...'); }, 300000); // To stop the interval clearInterval(interval);
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!