Answer the question
In order to leave comments, you need to log in
How to use setInterval inside async/await?
Sorry for the stupid question, but I'm not at all familiar with all this asynchrony.
How can I use spacing inside code like this (code below under the spoiler)? This is a Puppeteer that launches a headless browser, and logs in to the site from where I need to pull the data once every 500ms, something like this:
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://url.com');
// Auth
await page.click(selector.login.tab);
await page.click(selector.login.email);
await page.keyboard.type(settings.username);
await page.click(selector.login.password);
await page.keyboard.type(settings.password);
await page.click(selector.login.button);
// Waiting for loading
await page.waitFor(10000); // TO FIX: there we should wait for loaded element or selector
await page.evaluate(() => {
setInterval(() => {
return console.log(document.querySelector('div._captions > p._value').innerText + document.querySelector('div._captions p._hl-value').innerText);
}, 500);
});
//await page.screenshot({path: 'screenshot.jpg'});
await browser.close();
})();
Answer the question
In order to leave comments, you need to log in
await page.evaluate(() => {
return new Promise((resolve) => {
setInterval(() => {
console.log(document.querySelector('div._captions > p._value').innerText + document.querySelector('div._captions p._hl-value').innerText);
resolve();
}, 500);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question