Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

You can add a setTimeout to your mousedown handler:

document.onmousedown = () => setTimeout(focusPopup, 2000);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda