puppeteer:在继续下一行之前等待N秒 [英] puppeteer: wait N seconds before continuing next line
问题描述
我试过放评估函数中的 setTimeout
但它似乎被忽略了
console.log('等待前');
await page.evaluate(async()=> {
setTimeout(function(){
console.log('waiting');
},4000)
});
console.log('等待后');
此代码不等待,只需在等待和 >等待后
你知道怎么做吗?
你可以使用一点承诺函数,
函数延迟(时间){
返回新的承诺(function(resolve){
setTimeout(resolve,time)
});
}
然后,只要您想要延迟,请拨打电话。
console.log('等待前');
等待延迟(4000);
console.log('等待后');
如果必须使用puppeteer,请使用内置的waitFor函数。
await page.waitFor(4000)
如果您仍想使用page.evaluate,请在4秒后解决。你没有解决任何问题。
await page.evaluate(async()=> {
等待新的承诺( function(resolve){
setTimeout(resolve,1000)
});
});
但我想你可以简单地使用前两个例子。
in puppeteer I would like to wait a defined time before going to the next line of code.
I've tried to put a setTimeout
in an evaluate function but it seems to be simply ignored
console.log('before waiting');
await page.evaluate(async() => {
setTimeout(function(){
console.log('waiting');
}, 4000)
});
console.log('after waiting');
This code don't wait and just write before waiting and after waiting
Do you know how to do this?
You can use a little promise function,
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
Then, call it whenever you want a delay.
console.log('before waiting');
await delay(4000);
console.log('after waiting');
If you must use puppeteer use the builtin waitFor function.
await page.waitFor(4000)
If you still want to use page.evaluate, resolve it after 4 seconds. You are not resolving anything.
await page.evaluate(async() => {
await new Promise(function(resolve) {
setTimeout(resolve, 1000)
});
});
But I guess you can simply use the first two examples.
这篇关于puppeteer:在继续下一行之前等待N秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!