maca
maca

Reputation: 540

Recursive function not incremeanting its own parameter

var documentURLs = ["maca.pdf", "maca2.pdf"]
function printDocuments(index){
   if(index > documentURLs.length){
        return;
   }
   else {
     printJS({
       printable: documentURLs[index],
       type: 'pdf',
       onPrintDialogClose: function () {
         console.log(index);
          printDocuments(index++)
       }
     })
   }
}
<button type="button" onclick="printDocuments(0)">Print</button>

This is not incrementing the index, always print first document and it is not stopping

Upvotes: 0

Views: 43

Answers (1)

paolostyle
paolostyle

Reputation: 1988

It behaves exactly as it should. If you want to call printDocuments with the next index, you should use printDocument(++index) or justprintDocument(index+1), because it's the least confusing one. i++ returns current value of i and then adds 1. ++i first adds 1 and returns that increased value (so i+1).

Upvotes: 3

Related Questions