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

191
Views
¿Cómo omitir/borrar esta función sleep()?

 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } $('#target').click(function(e) { e.preventDefault(); // try to stop the countdown }); async function start() { for (index = 0; index < 5; index++) { await sleep(1000); $('#timer').text(index); } } start();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div style="width: 100px; height:100px; background-color: red" id="target"></div> <div id="timer"></div> </body> </html>

¿Hay alguna forma de borrar u omitir esta función de suspensión para que la cuenta regresiva se detenga cuando hago clic en un elemento específico (en este caso, el cuadrado rojo)?

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

0

En este caso, no necesita cancelar el tiempo de espera, simplemente ignórelo cuando se dispare.

Tenga una variable global (puede ser un espacio de nombres, por supuesto) que establezca en verdadero/falso cuando comience y falso/verdadero cuando desee detenerse.

Fragmento actualizado usando verdadero (activo) / falso (detenido).

También podría usar el opuesto var cancelled = false; luego configúrelo en verdadero cuando haga clic y compruebe si es falso.

 var active; function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } $('#target').click(function(e) { e.preventDefault(); // try to stop the countdown active = false; }); async function start() { active = true; for (index = 0; index < 5; index++) { await sleep(1000); if (!active) break; $('#timer').text(index); } } start();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div style="width: 100px; height:100px; background-color: red" id="target"></div> <div id="timer"></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!