两个动作之间的同步for-of和延迟

Synchronous for-of and delay between two actions

let files = document.querySelectorAll('#app .File');
let downloadButton = document.querySelector('.button-download');

for (let file of files) { 
    file.click(); // click file
    setTimeout(() => {}, 1000); // delay of 1 sec between two clicks
    downloadButton.click(); // click download 
}

..但它似乎不起作用。 for-of 同步运行,但它仍然只是多次下载最后一个文件。

如果你想要一个 "delay" 函数,你可以创建这样的东西:

function delay (ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}

之后,您需要做的就是将代码包装在 async 函数中,以便在调用 "delay" 函数时能够使用 await 关键字。

async function main () {
  let files = document.querySelectorAll('#app .File')
  let downloadButton = document.querySelector('.button-download')

  for (let file of files) { 
    file.click() // click file
    await delay(1000) // delay of 1 sec between two clicks
    downloadButton.click() // click download 
  }
}

main()