#Mysteries of setInterval

14 messages · Page 1 of 1 (latest)

junior kayak

Why the hell does _callback get executed the second time in setInterval even tho the interval was literally cleared whatthe

private static async _callback(){
        if (this._channels.size > 0 && typeof this._interval !== 'undefined'){
            const result = await this._channels.first()!.delete().catch(error => error);
            console.log('IF: #1', `this._interval: ${this._interval}`);

            if (result instanceof RateLimitError){
                console.log('IF: #2');
                this._interval = clearInterval(this._interval);
                this.duration += result.timeToReset;
                this._interval = setInterval(this._callback.bind(this), this.duration);

            } else {
                console.log('ELSE: #1')
                this._channels.delete(this._channels.firstKey()!);
                if(result instanceof DiscordAPIError) this._interval = (console.log('IF: #3'), clearInterval(this._interval));
              console.log(`this._interval: ${this._interval}`)  
            };

        } else if (this._channels.size === 0 && typeof this._interval !== 'undefined') this._interval = (console.log('IF: #4'), clearInterval(this._interval));
    };

Here are the console logs:

IF: #1 object Error
ELSE: #1
IF: #3
IF: #1 undefined Error
ELSE: #1
IF: #3
lone herald

Without seeing all of it (how you started the interval for example) this is kinda hard to say

junior kayak

am starting it just once in my enqueue method:

public static async enqueue(item: typeof this._channels){
        return await new Promise((resolve, reject) => {
            !item.every((value, key) => typeof key === 'string' && value instanceof BaseChannel)
            ? reject(new TypeError('Expected: Collection<string, GuildChannel>'))
            : resolve((this._channels = this._channels.merge(item, x => ({keep: true, value: x}), y => ({keep: true, value: y}), (z) => ({keep: true, value: z}))).size);

            if (typeof this._interval === 'undefined') (console.log('initializing interval'), this._interval = setInterval(this._callback.bind(this), this.duration));
        });
    };

my enqueue function is called everytime someone runs the /channel delete command

but of course, this is my test bot so only I run it once

lone herald

Why do you use (…,…) expressions instead of actual blocks there?

Maybe console.logging your _interval might help to shed some light on your issue

junior kayak
junior kayak
initializing interval

IF: #1 this._interval: 196
ELSE: #1
IF: #3
this._interval: undefined

IF: #1 this._interval: undefined
ELSE: #1
IF: #3
this._interval: undefined
lone herald

Oh wait, how high did you set the duration? You probably call your callback twice before the first delete call rejects/resolves

Because it waits for the ratelimit to pass

junior kayak

my current duration is 500ms even tho I also tested it on 2000ms. After deleting all channels except for the Community channels (which couldn't be deleted) and changing this.duration to 2000ms, it doesn't cause that weird behavior anymore sigh

seems like at lower duration, it causes that weird behavior