Search code examples
javascriptjqueryprintingpopupdisable-link

Disable a pop up link for 5 seconds using javascript


For a project I need to print a document using PHP code. Currently I have a self closing pop up to start the print.

The only problem I have is that a user could spam the button creating a lot of print requests and a huge queue.

The code I have right now:

function newPopup(url) {
popupWindow = window.open(
url,'popUpWindow','height=10,width=100,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=no');    // Verstop op achtergrond
popupWindow.blur();
}
<a href="JavaScript:newPopup('print.php');">Print</a>

I have found some code to stop links but I have problems implementing these since I already call it as a pop up.


Solution

  • You can use a flag:

    var flag=true;
    function newPopup(url) {
      if(flag) {
        window.open(...).blur();
        flag=false;
        window.setTimeout(function(){flag=true;},5*1000);
      }
    }
    

    Not a "good" solution (uses a global variable), but it should work.