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

102
Views
¿Cómo hacer una cuenta regresiva múltiple en una página usando jquery?

Necesito ayuda con el código javascript, trato de obtener un código que funcione para varios temporizadores de cuenta regresiva en la misma página. Intenté el siguiente código pero no tuve éxito.

 $(".expirydiv").each(function(){ var timer = $(this).attr("data-promo"); var countDownDate = new Date(timer).getTime(); var x = setInterval(function() { var now = new Date().getTime(); var distance = countDownDate - now; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); $(this).text(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); if (distance < 0) { clearInterval(x); $(this).text("EXPIRED"); } }, 1000); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="expirydiv" data-promo="2021/12/27 10:59:00"></div> <div class="expirydiv" data-promo="2021/12/19 05:09:00"></div>

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

0

$(esto) tiene un alcance dentro de setInterval, por lo que necesita una referencia:

 $(".expirydiv").each(function() { var timer = $(this).attr("data-promo"); var countDownDate = new Date(timer).getTime(); // HERE I CREATE A REFERENCE TO $(this) THAT IS PASSED TO SetInterval var $this = $(this); var x = setInterval(function() { var now = new Date().getTime(); var distance = countDownDate - now; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); $this.text(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); if (distance < 0) { clearInterval(x); $this.text("EXPIRED"); } }, 1000); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="expirydiv" data-promo="2021/12/27 10:59:00"></div> <div class="expirydiv" data-promo="2021/12/19 05:09:00"></div>

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!