Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!