0

我想创建一个while循环,只要超时完成或满足条件,它就会中断

async checkFile (filePath) {
    const readDir =  fs.readdirSync(filePath,'utf-8')
    while(true) {
        if(readDir === filename) {
            break;
        }
        
    } 
}
4

1 回答 1

0

这样的事情怎么样?创建一个开始日期变量,然后循环条件是当前日期 - 开始日期小于 1000(毫秒)或您希望的任何超时

async checkFile(filePath) {
    const readDir =  fs.readdirSync(filePath,'utf-8');
    const start = new Date();
    while ((new Date()) - start < 1000) {
        if(readDir === filename) {
            break;
        }

    }
}

于 2021-10-28T22:14:48.043 回答