[英]How to set a interval with Puppeteer to run a certain amount of time?
I would like to set an interval, so the code updates every second, but only for 2 minutes.我想设置一个间隔,所以代码每秒更新一次,但只有 2 分钟。
I am not sure how to set this up.我不确定如何设置。
const puppeteer = require('puppeteer');
(async () => {
let game = 'https://lichess.org/hj4DgCNN9BKG';
let browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();
await page.goto(game, { waitUntil: 'networkidle2' });
let data = await page.evaluate(() => {
let time = document.querySelector('div[class="time"]').innerText;
return { time
};
});
console.log(data);
})();
You can try something like this:你可以尝试这样的事情:
const puppeteer = require('puppeteer');
(async () => {
let game = 'https://lichess.org/hj4DgCNN9BKG';
let browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();
await page.goto(game, { waitUntil: 'networkidle2' });
const end = Date.now() + 120000;
while (Date.now() < end) {
let data = await page.evaluate(() => {
let time = document.querySelector('div[class="time"]').innerText;
return { time };
});
console.log(data);
await page.waitForTimeout(1000);
}
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.