Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda