简体   繁体   中英

Print preview freeze intervals in javaScript

I have a problem with method mywindow,print(); cause when print preview is open my intervals in JavaScript files stops. My code is :

function pri() {

  var mywindow = window.open('bileta.html', 'hello', 'height=400,width=600');
  mywindow.document.write('<html><head><title></title>');
  mywindow.document.write('</head><body >');
  mywindow.document.write('<h1></h1>');
  mywindow.document.write(document.getElementById("hello").innerHTML);
  mywindow.document.write('</body></html>');

  mywindow.document.close(); // necessary for IE >= 10
  mywindow.focus(); // necessary for IE >= 10*/

  mywindow.print();
  mywindow.close();

  return true;
}

I've searched in google and stalckoverflow but all the answers were about the chrome version and I use a chrome version 58.0.3029.110 (64-bit).

So what I'm asking is that is any way to print and my print preview page not freeze my JavaScript intervals?

As mentioned in my comment above I believe the single thread of Javascript is blocked by the window.print() call you are making so a normal interval you set will pause.

A web worker opens a second thread up, that can still keep it running. My script looks like this:

<!doctype html>
<html>
    <head></head>
    <body>

        <p>Press to <a href="javascript:pri()">print</a>!</p>
        <p id="countup">Count</p>

        <script>

            window.onload = function() {
                if (window.Worker) {
                    var myWorker = new Worker('worker.js');
                    myWorker.onmessage = function(msg) {
                        console.log(msg.data);
                        document.getElementById('countup').innerHTML = msg.data;
                    };
                }
            }

            function pri() {
                var mywindow = window.open('bileta.html', 'hello', 'height=400,width=600');
                mywindow.document.write('<html><head><title></title>');
                mywindow.document.write('</head><body >');
                mywindow.document.write('<h1 id="hello">Hello</h1>');
                mywindow.document.write('</body></html>');

                mywindow.document.close(); // necessary for IE >= 10
                mywindow.focus(); // necessary for IE >= 10*/

                mywindow.print();
                mywindow.close();

                return true;
            }
        </script>
    </body>
</html>

And the worker.js script looks like this:

function counter() {

    var count = 0;

    self.setInterval(function() {
        postMessage('counter =' + count);
        count++;
    }, 100);

 }

 counter();

Of course printing involves interacting with the browser and OS so results may vary. This script works with Chrome, IE11, Edge on Windows 10 but not Firefox (perhaps somebody could post a comment fixing that.)

See these two articles for more information on web workers:

MDN Using Web Workers

Basics of Web Workers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM