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

187
Visualizações
setTimeout not waiting in class constructor

I am trying to set a timeout in a class constructor, to turn of the emitter, but when calling new Collector(options) it doesn't wait and immediately runs the .stop() function, which it should only do after 15000 ms.

My code:

Collector.js:

module.exports = class Collector extends EventEmitter {
    #counter;
    #ended;
    #timeout;
    #timeoutTime;
    /**
     * 
     * @param {{ filter?: CollectorFilter, max?: number, type: string, time?: number}} options 
     */
    constructor(options, client) {
        super();
        this.#counter = 0;
        this.collected = new Collection();
        this.filter = options.filter ?? (() => true);
        client.collectors.set(options.type, this);
        this.max = options.max ?? 1;
        this.#ended = false;
        this.#timeoutTime = options.time ?? 15000;
        console.log(this.#timeoutTime)
        this.#timeout = setTimeout(() => {
            console.log('hey')
            this.stop('TIME_END')
        }, this.#timeout);
    }
    /**
     * 
     * @param  {MessageComponentInteraction | ModalInteraction} interaction 
     */
    collect(interaction) {
        if(this.#ended) return;
        const filterBoolean = this.filter(interaction)
        if(filterBoolean) {
            this.#counter++
            this.collected.set(interaction.id, interaction)
            this.emit('collect', interaction);
            if(this.#counter === this.max) {
                this.emit('end', this.collected, 'MAX');
                this.#ended = true;
            }
        }
        else {
            this.emit('ignore', interaction)
        }
    }

    stop(reason = 'user') {
        this.#ended = true;
        clearTimeout(this.#timeout);
        this.emit('end', this.collected, reason)
    }
    
}

Calling the constructor:

const collector = new Collector({ filter: (i) => i.customId === 'hey',  max: 2}, client, );
about 4 years ago · Juan Pablo Isaza
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