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

169
Views
how do you do something after a certain time has passed?

i'm trying to make an alarm in JS where i get the value of an input and Multiply it by 1000 to get the ms value and setTimeout a function which changes a paragraph element to become Time Up, the time in the setTimeout is the value of the input x 1000 (because miliseconds) Somewhat like this:

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

}

instead of doing the expected for me (doing the function after the time set by alert.value * 1000 has passed) it instantly outputs Time Up. am I misunderstanding anything? because I searched on Google for like 1 hour and I couldn't find anything aside from setTimeout and setInterval.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

the problem is

setTimeout(domChanger(), alert.value)

where domChanger() is executed immediatly

you need to pass function reference

 setTimeout(domChanger, alert.value)

demo

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

You don't need to pass the time as parameter. Just do the code bellow.

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!