【问题标题】:setTimeout random timesetTimeout 随机时间
【发布时间】:2021-11-11 16:43:32
【问题描述】:

您好,我怎样才能将其更改为 30-50 秒之间的随机时间? 我不需要新代码或只是想编辑它


client.on('chat', function(channel, user, message, self) {
    if (user.username == 'Cortex' && message === 'sar') {
        
        test();
        
    }
});


function test() 
{
    setInterval(function() {
        var spamMessage = 'KEK ';
        var toSpam = 'MingLee';
        for (i = 0; i < 30; i++) {
            spamMessage += toSpam + ' '; 
        }
        client.say(channel, spamMessage)
    }, 30000);
} 

【问题讨论】:

标签: javascript twitch-api


【解决方案1】:

这样的?

client.on('chat', function(channel, user, message, self) {
    if (user.username == 'Cortex' && message === 'sar') {
        
        test();
        
    }
});


function test() 
{
    const min = 30000;
    const max = 50000;

  const time =  Math.floor(Math.random() * (max - min)) + min;

    setInterval(function() {
        var spamMessage = 'KEK ';
        var toSpam = 'MingLee';
        for (i = 0; i < 30; i++) {
            spamMessage += toSpam + ' '; 
        }
        client.say(channel, spamMessage)
    }, time);
} 

【讨论】:

  • 我猜 OP 每次都希望它是随机的,每次都不一样(不是反对票)
猜你喜欢
  • 2011-01-19
  • 1970-01-01
  • 2014-06-01
  • 2011-11-26
  • 1970-01-01
  • 2017-05-21
  • 2013-03-15
  • 2011-03-26
  • 2021-07-30
相关资源
最近更新 更多