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

161
Views
¿Cómo haces algo después de que ha pasado cierto tiempo?

Estoy tratando de hacer una alarma en JS donde obtengo el valor de una entrada y lo multiplico por 1000 para obtener el valor de ms y setTimeout una función que cambia un elemento de párrafo para convertirse en Time Up, el tiempo en setTimeout es el valor de la entrada x 1000 (porque milisegundos) Algo así:

 function domChanger() { document.getElementById("paragraph").innerHTML = "Time Up"; } function time(alert) { alert = document.getElementById("input") alert.value *= 1000; setTimeout(domChanger(), alert.value) }

en lugar de hacer lo esperado por mí (hacer la función después de que haya pasado el tiempo establecido por alert.value * 1000) instantáneamente genera Time Up. ¿Estoy malinterpretando algo? porque busqué en Google durante 1 hora y no pude encontrar nada aparte de setTimeout y setInterval.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

el problema es

 setTimeout(domChanger(), alert.value)

donde domChanger() se ejecuta inmediatamente

necesitas pasar la referencia de la función

 setTimeout(domChanger, alert.value)

manifestación

 function domChanger() { document.getElementById("paragraph").innerHTML = "Time Up"; } function time() { var alert = document.getElementById("input") alert.value *= 1000; setTimeout(domChanger, alert.value) }
 <p id="paragraph"></p> <input id="input" type="number" step="1" min="1" value="2"> <button type="button" onclick="time()">time</button>

about 4 years ago · Santiago Gelvez Report

0

No es necesario pasar el tiempo como parámetro. Solo haz el siguiente código.

HTML

 <p id="paragraph"></p> <input type="number" id="input"> <button onclick="time()">Lauch domChanger</button> <script src="./index.js"></script>

JS

 function domChanger() { document.getElementById("paragraph").innerHTML = "Time Up"; } function time() { alert = document.getElementById("input"); alert.value *= 1000; setTimeout(domChanger, alert.value);; }
about 4 years ago · Santiago Gelvez 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!