如何在同步函数中停止JavaScript中的函数?

如何在同步函数中停止JavaScript中的函数?,javascript,timeout,jspdf,Javascript,Timeout,Jspdf,大家好,我正在用这段代码以横向模式打印pdf,每边打印两个数据集,这件事做得很好,但现在我想用一个停止按钮来停止这个过程 我被它卡住了,所以请给我一些建议 提前感谢,这是我第一次在这个平台上发布问题 function timer(ms) { return new Promise(res => setTimeout(res, ms)); } async function load() { // wrap the

大家好,我正在用这段代码以横向模式打印pdf,每边打印两个数据集,这件事做得很好,但现在我想用一个停止按钮来停止这个过程

我被它卡住了,所以请给我一些建议

提前感谢,这是我第一次在这个平台上发布问题

function timer(ms) {
                return new Promise(res => setTimeout(res, ms));
            }

            async function load() { // wrap the loop into an async function for this to work
                for (i = 0; i < sCustID.length; i++) {
                    var elem = document.getElementById("myBar");
                    document.getElementById("pdfhead").innerHTML = qrtype;
                    document.getElementById("QRCODEID").innerHTML = sCustID[i];
                    document.getElementById("NAME").innerHTML = sName[i];
                    qrinput[i] = sCustID[i] + "," + sName[i] + "," + sType[i];
                    generateQRCode(qrinput[i]);
                    document.getElementById("printed").innerHTML = i;
                    document.getElementById("pdfhead2").innerHTML = qrtype;
                    document.getElementById("QRCODEID2").innerHTML = sCustID[i + 1];
                    document.getElementById("NAME2").innerHTML = sName[i+1];
                    qrinput[i+1] = sCustID[i+1] + "," + sName[i+1] + "," + sType[i+1];
                    generateSecQRCode(qrinput[i + 1]);
                    document.getElementById("printed").innerHTML = i + 1;
                    PdfFDesignModal.style.display = "block";
                    TimerModal1.style.display = "block";
                    width = ((i+1) * 100) / totalprint1;
                    elem.style.width = width + '%';
                    i = i + 1;
                    savePdf(sCustID[i + 1]);
                    if (i  == sCustID.length) {
                        alert("All QRCODE Printed");
                        PdfFDesignModal.style.display = "none";
                        TimerModal1.style.display = "none";
                    }
                    await timer(1500); // then the created Promise can be awaited
                }
            }
            load();

        }
功能计时器(毫秒){
返回新承诺(res=>setTimeout(res,ms));
}
async function load(){//将循环包装到异步函数中,以使其工作
对于(i=0;i
设置一个带有侦听器的按钮,该侦听器调用函数来设置全局变量。然后在for循环中将该变量作为条件测试的一部分进行测试

这将起作用,因为for循环具有异步性质。调用
stopPrint()
将有机会中断该过程

let stop=false;
document.getElementById('stop')。addEventListener('click',stopPrint);
功能计时器(毫秒){
返回新承诺(res=>setTimeout(res,ms));
}
异步函数加载(){
对于(i=0;i<10&&!stop;i++){
//你在这里工作吗
控制台日志(i);
等待定时器(1500);
}
}
加载();
函数stopPrint(){
停止=真;
}

Stop
使用自定义承诺类时,代码可能如下所示

import CPromise from "c-promise2";

async function yourAsyncTask(i) {
  //some async code here
  console.log(`Task [${i}]`);
  return 123;
}

function load() {
  return CPromise.from(function* () {
    this.innerWeight(20); // for progress capturing, can be omitted
    for (let i = 0; i < 10; i++) {
      yield yourAsyncTask(i); //use yield with promises instead of await here
      yield CPromise.delay(1000); // I don't know why you need a delay
    }
  });
}

const cancelablePromise = load()
  .progress((value) => console.log(`Progress: ${value * 100}%`)) // just for fun let's capture progress
  .then(
    () => console.log("Done"),
    (err) => console.log(`Fail: ${err}`)
  );

setTimeout(() => {
  cancelablePromise.cancel(); // abort async sequence
}, 3500);
Task [0] 
Progress: 5%
Progress: 10%
Task [1] 
Progress: 15%
Progress: 20%
Task [2] 
Progress: 25%
Progress: 30%
Task [3] 
Progress: 35%
Fail: CanceledError: canceled