Can JavaScript execute functions in the background?
I am using the pdfmake tool to generate a PDF within an AngularJS application, but the generation process takes around 3-4 seconds and causes the UI to freeze completely.
I want to run a background task to initiate the PDF download without impacting the user interface. Is this achievable?
This is how I currently implement pdfmake (with custom factories named pdfmake
and _
):
'use strict';
angular.module('App')
.service('CatalogPdfService', ['pdfmake', '_', '$q', '$filter',
function (pdfmake, _, $q, $filter) {
var $translate = $filter('translate');
var listDate = new Date();
return {
download: download
};
function download(data) {
listDate = _.first(data).publishedOn;
console.log('initiating download process');
var deferred = $q.defer();
var filename = $translate('APP.EXPORT.pdf.catalog.title', {date: $filter('amDateFormat')(listDate, 'DDMMYYYY')}) + '.pdf';
create(data).download(filename, function () {
console.log('download completed');
deferred.resolve();
});
return deferred.promise;
}
// Other code implementation remains unchanged...
}]);