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

134
Visualizações
How to add a delay to onmousedown

I am trying to set a delay on the mousedown (click) which focuses the newWin.focus in code below: So in short, the code below works fine. If the popup window is open, it re-focuses on it (bringing it back front) when the user clicks anywhere on the page.

I would like to have a 2 second delay when the user clicks on the page, THEN have it refocus on the new window.

I have spent hours trying to figure this out to no avail using various settimeout's here and there. So, I thought I would show what I have that works and hope someone might explain how my need can be accomplished.

PS: I am still learning my way around javascript and in NO WAY consider myself knowledgeable in the subject.

Thanks in advance!!

<script type="text/javascript">
var newWin;
function openPopup()
{
 newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');

 document.onmousedown=focusPopup;
 document.onkeyup=focusPopup;
 document.onmousemove=focusPopup;
}
function focusPopup(){
  if(!newWin.closed){
    newWin.focus();
  }
}
</script>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Just use setTimeout to add the delay.

The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

var newWin;

function openPopup() {
  newWin = window.open('https://www.somesite/url.php', 'window', 'width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');

  document.onmousedown =  function() {
    setTimeout(focusPopup, 2000);
  };
  document.onkeyup = focusPopup;
  document.onmousemove = focusPopup;
}

function focusPopup() {
    if (!newWin.closed) {
      newWin.focus();
    }
}

about 4 years ago · Juan Pablo Isaza Relatório

0


<script type="text/javascript">
var newWin;
function openPopup()
{
 newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');

 document.onmousedown=()=>{
   setTimeout(focusPopup,2000)

};
 document.onkeyup=focusPopup;
 document.onmousemove=focusPopup;
}
function focusPopup(){
  if(!newWin.closed){
    newWin.focus();
  }
}
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can add a setTimeout to your mousedown handler:

document.onmousedown = () => setTimeout(focusPopup, 2000);
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