【问题标题】:How do Find the Time Left in a setTimeout() ? Javascript如何在 setTimeout() 中找到剩余时间? Javascript
【发布时间】:2020-09-25 07:07:46
【问题描述】:

您好,我的问题很清楚。我有一个 setTimeout。当有人使用命令时,计时器被激活。如何找到 setTimeout() 中剩余的时间?找了一些资源,但是没用,有些资源需要时间到NaN

我能做什么?你们能帮帮我吗?

例子:waitsetTimeOut.setDescription('剩余时间2分钟')

这是我的代码,我的代码是最新的。

const Discord = require("discord.js");
const fetch = require("node-fetch");
const hereTimeOut = new Set();
let user = 1;

exports.run = async (bot, message, args) => {
    if (hereTimeOut.has(user)) {

        const waitsetTimeOut = new Discord.RichEmbed()
        waitsetTimeOut.setColor(0x00AE86)
        waitsetTimeOut.setTimestamp()
        waitsetTimeOut.setAuthor(message.author.username, message.author.avatarURL)
        waitsetTimeOut.setTitle("[wait a while]")
        waitsetTimeOut.setDescription('please wait 1 minute')
        return message.channel.sendEmbed(waitsetTimeOut);
    }else {


    let country = args.slice(0).join(' ');

    if(!country){

        fetch("https://covid19.mathdro.id/api/").then(res => res.json()).then(json => {

            const embed = new Discord.RichEmbed();
                embed.addField("**= Total Number of Cases =**",`**`+ json.confirmed['value'] +` person**`)
                embed.addField("**= Number of Healing Cases =**",`**`+ json.recovered['value'] +` person**`)
                embed.addField("**= Number of Cases Losing Life =**",`**`+ json.deaths['value'] +` person**`)
                embed.setColor(15962634)
                embed.setTitle('Worldwide COVID-19 Statistics')
            message.channel.send({embed: embed});

        });

    }else{

        fetch(`https://covid19.mathdro.id/api/countries/${country}`).then(res => res.json()).then(json => {

                const embed = new Discord.RichEmbed();
                    embed.addField("**= Total Number of Cases =**",`**`+ json.confirmed['value'] +` person**`)
                    embed.addField("**= Number of Healing Cases =**",`**`+ json.recovered['value'] +` person**`)
                    embed.addField("**= Number of Cases Losing Life =**",`**`+ json.deaths['value'] +` person**`)
                    embed.setColor(15962634)
                    embed.setTitle(`COVID-19 Statistics (${country})`)
                message.channel.send({embed: embed});

        }).catch(() => {

            message.reply("I couldn't find the country you are looking for, be careful not to use Turkish letters when writing the country. You can also write country abbreviations (ex: TR, USA, AZ)");

        });

    }

    hereTimeOut.add(user);
        setTimeout(() => {
          hereTimeOut.delete(user);
        }, msConvert(country));
    }

};

exports.conf = {
  enabled: true,
  guildOnly: true,
  aliases: ['corona'],
  permLevel: 0
};

exports.help = {
  name: "corona",
  description: "covid19",
  usage: "coronavirus"
};

【问题讨论】:

标签: javascript discord discord.js


【解决方案1】:

我知道我迟到了,但这应该可以(我猜)

GetTimeLeft(timeout) => {
    return(Math.ceil((timeout._idleStart + timeout._idleTimeout - Date.now()) / 1000));
}
let Timeout = setTimeout(() => {
    //Your code here
}, 3600000);

然后你可以在任何地方调用它

console.log(GetTimeLeft(Timeout);

在你的情况下输入一个命令

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2021-10-26
    • 2011-03-09
    • 1970-01-01
    • 2013-01-22
    • 2013-02-04
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多