nw.js close window after using it to print

Viewed 122

I am printing data from a new window after which I want to close the window. If I close the window straight away then it doesn't have enough time for the javascript function to run which writes data to the document. I am using setTimeout to mimic this behaviour. However, I am wondering if there is a native way to do this.

For obvious reasons setting time out isn't an optimal solution as many things could block execution etc - perhaps a callback from the print function would do the trick, docs do not mention anything about it though. Here's my code:

nw.Window.open('print.html', {show: false, new_instance: false}, function(new_win) {
    new_win.on('loaded', function() {
      this.print({autoprint: true, headerFooterEnabled: false});
        });
    });

And in the print.html:

var div = document.getElementById("orderBill");
div.innerHTML = '<h1>Header</h1><p>Some text</p>';
var win = nw.Window.get();
setTimeout(function(){ win.close(); }, 5000);
1 Answers
Related