-2

So I made this function that works, but how can I make the message update every second. I've tried to use setInterval(countdownTimer(), 1000), but it doesn't work. Here is my code!

let x = await msg.channel.send('Calculating...')
    async function countdownTimer() {
        const difference = +new Date("2020-06-01") - +new Date();
        let remaining = "Time's up!";

        if (difference > 0) {
          const parts = {
            days: Math.floor(difference / (1000 * 60 * 60 * 24)),
            hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
            minutes: Math.floor((difference / 1000 / 60) % 60),
            seconds: Math.floor((difference / 1000) % 60)
          };

          remaining = Object.keys(parts)
            .map(part => {
              if (!parts[part]) return;
              return `${parts[part]} ${part}`;
            })
            .join(" ");
        }
        setInterval(() => {
            x.edit(remaining);
        }, 1000);
    }   
    countdownTimer()
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Massqerix
  • 1
  • 2
  • 1
    try `setInterval(countdownTimer,1000)`. It's not working as expected because the function is being invoked as soon as setInterval runs. – Rohit Kashyap May 23 '20 at 08:41
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) Tags don't go in question titles (that's what tags are for!). Also, "it doesn't work" isn't a useful problem description -- say *what* you're seeing that isn't right. – T.J. Crowder May 23 '20 at 08:43
  • 1
    I've also tried to fix the formatting of the question, but your code seems to start in the middle of a block (you have `countdownTimer();` and `}` after the end of the function, with nothing matching up to them. – T.J. Crowder May 23 '20 at 08:43
  • 2
    Hi! First of all, it's not appreciated here to say you need something fast. We are all volunteering this. (If you need it fast, please pay my regular $110/hr rate plus a $50 urgency surcharge...) But regardless, the solution is probably to remove the `()` in your `setInterval` because you don't want to execute the function now and use its return value but instead you want to pass a reference to the function itself so that `setInterval` can repeatedly call it for you. – CherryDT May 23 '20 at 08:43

1 Answers1

0
function countdownTimer() {
    const difference = +new Date("2020-06-01") - +new Date();
    let remaining = "Time's up!";
    if (difference > 0) {
        const parts = {
            days: Math.floor(difference / (1000 * 60 * 60 * 24)),
            hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
            minutes: Math.floor((difference / 1000 / 60) % 60),
            seconds: Math.floor((difference / 1000) % 60)
        };

        remaining = Object.keys(parts)
            .map(part => {
                if (!parts[part]) return;
                return `${parts[part]} ${part}`;
            })
            .join(" ");
    }
    return remaining;
}
setInterval(() => {
    const remaining = countdownTimer();
    x.edit(remaining);
}, 1000)
gu mingfeng
  • 1,010
  • 8
  • 10