Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
setInterval doesn't work without an error

So i tried to watch a tutorial and make a web scraper, i combined like 5 tutorial worth code into my code so i suppose it's messy.

This is the code:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const fs = require('fs');
const { html } = require('cheerio');
const cheerio = require('cheerio');
const { setInterval } = require('timers/promises');
require('dotenv').config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid,authToken);
puppeteer.use(StealthPlugin());
const oldData = "oldData";

async function scrape(){
    const browser = await puppeteer.launch({
        headless: true,
        defaultViewport:{
            width: 1920,
            height: 1080
        }
    });


    const page = await browser.newPage();
    await page.goto('page');


    const pageData = await page.evaluate(() => {
        return{
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight,
            html: document.documentElement.innerHTML,
        };
    })

    const $ = cheerio.load(pageData.html);
    
    const element = $(".accordion__header-inner:last");
    console.log(element.text());
};

const handle = setInterval(scrape(), 4000);
scrape();

I hide some parts that i didn't want to be seen.

So, when i run the code function it logged the element thing twice. When i delete the interval thing it works only one. That is the only difference, i want to log the element thing in every 5 seconds.

Any help is appreciated and i hope this isn't a poorly written question.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your setInterval you are executing the function and passing it's return value as argument, instead of passing function itself as argument. It should be:

const handle = setInterval(scrape, 4000);

Note, you won't be use this inside the function, because it will be called inside setInterval scope. If you need to use this, then use bind():

const handle = setInterval(scrape.bind(scrape), 4000);
about 4 years ago · Juan Pablo Isaza Relatório

0

You seem to be calling the "scrape()" method in two places. Once in the SetInterval() method and then immediately again on the line after. That looks like your problem.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda