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 to make prompt appear only after event listener finishes?

What I want to do is I want to make a prompt question if I want to continue(after temp result) or not if not then quit the page BUTTT whenever I put prompt or alert, they appear first instead of the value of the temperature.

const result = document.querySelector(".result") /// button
const change = document.querySelector(".change") /// button
const screen = document.querySelector(".ctF") //h1
const tscreen = document.querySelector(".screen") ///h1
const dta = [{
  index: 1,
  message: "Celcius to Fahrenheit"
}, {
  index: 2,
  message: "Fahrenheit to Celcius"
}]
let num = 0
let iV = 1

change.addEventListener("click", () => {
  num++
  if (num > dta.length - 1) {
    num = 0
  }
  t(num)
})

function t(ss) {
  let item = dta[ss]
  let message = item.message
  let index = item.index
  iV = index
  tscreen.innerHTML = message
  screen.innerHTML = "Temperature"
}

result.addEventListener("click", changeTemp)

function changeTemp() {
  const value = document.querySelector(".input").value ///input
  let numbaW = 1
  let numbaT = 2
  if (numbaW == iV) {
    let cetFa = value * (1.8) + 32
    screen.innerHTML = Math.round(cetFa) + "°F"
    window.addEventListener("message")
  } else if (numbaT == iV) {
    let fatCe = ((value - 32) * 5) / 9
    screen.innerHTML = Math.round(fatCe) + "°C"
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

prompt and alert are both halting the JavaScript event loop.
This means as long as the user does not click on "ok" no DOM modifications will be processed.
The solution will be to prompt the user after a timeout.

Ususally

setTimeout(() => {
  let value = prompt("What's the value?");
  alert(`Value: ${value}`);
});

should do the trick for you.

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!