我该如何延迟setActivity

我已经尝试过了,但实际上并没有用... 我希望它重复并延迟500ms 像H He Hel Hell你好,重复一遍

const Discord = require('discord.js');
const c = new Discord.Client();
const token = '';

c.on('ready', () => {
    console.log('Logged in.')
    const a = [
        'H',
        'He',
        'Hel',
        'Hell',
        'Hello'
    ]

    let i = 0

    setTimeout(() => {
        c.user.setActivity(a[i])
        i++
    }, 2000);
})

c.login(token)

我认为这应该有效...告诉我我做错了..

评论
svelit
svelit

You need setInterval rather than setTimeout:

var timer = setInterval(() => {
    c.user.setActivity(a[i]);
    i++;
    if (i == a.length) clearInterval(timer);
}, 2000);

setTimeout executes once, setInterval executes infinitely (which is why you need the if and clearInterval with variable reassignment).

点赞
评论