1

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>

这不会增加索引,始终打印第一个文档并且它不会停止

4

1 回答 1

3

它的行为与应有的完全一样。如果你想printDocuments用下一个索引调用,你应该使用printDocument(++index)or printDocument(index+1),因为它是最不容易混淆的。i++返回的当前值i然后加 1。++i首先加 1 并返回增加的值 (so i+1)。

于 2021-11-15T21:58:59.233 回答