Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

You can add a setTimeout to your mousedown handler:

document.onmousedown = () => setTimeout(focusPopup, 2000);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading