-2

FIXED THANKS TO @Francois Wahl - The timeout solutions worked. I have a href in my option to print which was stopping it from working. By removing it the print page option works with a two second delay. Thankyou everyone!!!!!

I have the following script and need to add a delay after WinPrint.focus(); in order to wait a second or two before it runs WinPrint.print();.

I have tried numerous methods of delays from this and other forums with no luck. Any help would be very much appreciated.

When I use the setTimeout function, it stops the code within it from running.

<script type="text/javascript">
$('a.print-pdf-link').click(function () {
   var prtContent = document.getElementById("page-content");
    var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
    WinPrint.document.write(prtContent.innerHTML);
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();
});
</script>

I have tried the following:

<script type="text/javascript">
   $('a.print-pdf-link').click(function () {

       var prtContent = document.getElementById("page-content");
       var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
       WinPrint.document.write(prtContent.innerHTML);
       WinPrint.document.close();
       WinPrint.focus();
       setTimeout(function(){
       WinPrint.print();
       WinPrint.close();
       }, 2000)

   });
 </script>

And

<script type="text/javascript">
$('a.print-pdf-link').click(function () {
        var prtContent = document.getElementById("page-content");
        var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
        var myVar;

function myFunction() {
    myVar = setTimeout(alertFunc, 3000);
}
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        function alertFunc() {
        WinPrint.print();
        WinPrint.close();
        }
});
</script>

And

<script type="text/javascript">
$('a.print-pdf-link').click(function () {
        var prtContent = document.getElementById("page-content");
        var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        setTimeout(() => WinPrint.print(), 1000);
        WinPrint.close();
});
</script>
  • 5
    `setTimeout(() => WinPrint.print(), 1000)` maybe? – Luka Jacobowitz Dec 21 '16 at 09:10
  • Adding to the above, you can't just add a delay in between. You can specify what to execute after a certain delay, which is what the above snippet from Luka does. – kkaosninja Dec 21 '16 at 09:11
  • @LukaJacobowitz Please consider making that an answer – Flying Gambit Dec 21 '16 at 09:11
  • Possible duplicate of [How to set time delay in javascript](http://stackoverflow.com/questions/17883692/how-to-set-time-delay-in-javascript) – Nope Dec 21 '16 at 09:12
  • Thanks for the suggestion, but when I add that it causes the code to skip the print option and moves straight to close. I tried adding the same code around the close option with a slightly long delay and then the close option doesn't run. Any more ideas? – GlobeHead87 Dec 21 '16 at 09:14
  • @Francois Wahl, I have already tried the solution in your link to no avail. Thanks though. – GlobeHead87 Dec 21 '16 at 09:15
  • @BigHead87 So, you are saying that `setTimeout` is not going to solve your problem? You asked how to delay the execution of a function after another, if you got other issues that prevent you from using `setTimeout` you need to add the **exact** code you tried so we can see it and possibly help with it. You might have had a logical issue in it? – Nope Dec 21 '16 at 09:19
  • Yes, sorry should have mentioned it. I have added it to the original question. – GlobeHead87 Dec 21 '16 at 09:21
  • Not sure what the issues is, `setTimeout` as specified in one of the answers works fine in a fiddle ► https://jsfiddle.net/y75rm2qn/ Note that while the print dialog pops up, the `close` action will only work when a few conditions are met as outlined here ► http://www.w3.org/TR/html51/browsers.html#dom-window-close – Nope Dec 21 '16 at 09:26
  • @Francois Wahl, I have added some examples of what I have tried. I have put it in various formats to no avail. – GlobeHead87 Dec 21 '16 at 09:30
  • @BigHead87 Use the isolated code in the fiddle I posted above, which does pop up the print dialog on the new window just fine using the first solution you listed. If that works you have a different issue you need to figure out. – Nope Dec 21 '16 at 09:32
  • @Francois Wahl, hmmmm, interesting, it does indeed work on the fiddle. I'm missing something somewhere because when I copy your code to my page it just loads the about:blank window and stops. – GlobeHead87 Dec 21 '16 at 09:36
  • @Francois Wahl, thank you for all your help, turns out it was a href in the link i was using for the print button. Putting it in like you have has sorted it. – GlobeHead87 Dec 21 '16 at 09:38

2 Answers2

1

You can use setTimeout for this. Try this may be it helps you.

  <script type="text/javascript">
   $('a.print-pdf-link').click(function () {

       var prtContent = document.getElementById("page-content");
       var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
       WinPrint.document.write(prtContent.innerHTML);
       WinPrint.document.close();
       WinPrint.focus();
       setTimeout(function(){
       WinPrint.print();
       WinPrint.close();
       }, 2000)

   });
 </script>
Manoj Lodhi
  • 978
  • 4
  • 10
0

Use setTimeout method.

$('a.print-pdf-link').click(function () {
    setTimeout(function () {
        var prtContent = document.getElementById("page-content");
        var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
    }, 10);
});

This will execute this method after 10 milli second.

Nitheesh
  • 19,238
  • 3
  • 22
  • 49