Home > Blockchain >  Recursive function not incremeanting its own parameter
Recursive function not incremeanting its own parameter

Time:11-16

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

CodePudding user response:

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).

  • Related