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

126
Views
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 answers
Answer question

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 Report

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 Report

0

You can add a setTimeout to your mousedown handler:

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