0

I'm experiencing quite long freezes of Internet Explorer while it's executing long run tasks. The task is about generating large PDF document using pdfmake and pure/vanilla javascript. So, that means I dont have control of what this script do, I just call it:

function getPdf(docDefinition, filename) {
    var t = Date.now();
    return pdfMake.createPdf(docDefinition).download(fileName + '.pdf', function() {
        console.log('pdfMake.createPdf: \'' + fileName + '.pdf\' generated in: ' + (Date.now() - t) + 'ms');
    });
}

For some large docs it take more than 30sec while I cant do anything.

How to prevent of freezing, so I could display some spinning wait icon, or somthing else?

werasquez
  • 99
  • 2
  • 10
  • the only way is to use web worker. Otherwise, just put the process to create PDF into server. Browser just need to get the file. – Kim Hoang Aug 02 '17 at 10:38
  • If the library doesn't provide an asynchronous way of doing it, you are probably out of luck. As others have hinted at, if it were your own code that is locking it up it might make sense to shift some of the work to a web worker (although in this case I'm not sure that it would because all the data would need to be copied in and out of the worker via the structured clone algorithm, which is slow) or split the work into several pieces over several `setTimeout` calls; if it is the library's processing that is the bottleneck you can't do anything about in your code. – Useless Code Aug 02 '17 at 10:47
  • Possible duplicate of [Prevent long running javascript from locking up browser](https://stackoverflow.com/questions/672732/prevent-long-running-javascript-from-locking-up-browser) – Oliver Salzburg Aug 02 '17 at 10:49

2 Answers2

1

Since version 10 IE supports web-workers which allow running JS on a different thread than the main thread.

thedude
  • 9,388
  • 1
  • 29
  • 30
  • It looks like what i was looking for. But now comes a new problem: how to pass `docDefinition` (which has some functions inside) to worker ?? It only allowes serialized data. – werasquez Aug 02 '17 at 12:21
-1

You can use async function returning a promise, which will be resolved when pdf generator finishes his work.

ES5

You can use this promise polyfill

Marcin Pevik
  • 163
  • 1
  • 14