Home > other >  moment.js is not updating the time, only shows the time when .js file was started
moment.js is not updating the time, only shows the time when .js file was started

Time:04-01

I'm trying to include actual date and time to the certain message, however I'm unable to do it, because moment.js keeps stucked at the same time. For some reason it only triggers the same time over and over again when script was started.

//require moment   time format
var moment = require('moment');
var todaymoment = moment().format('L, hh:mm:ssa');

//Check the current time each 5 seconds and update message
      setInterval(function () {
        message.edit({ embeds: [currentTime.setDescription('Current time is: '   todaymoment) && currentTime.setTitle('Time Updated!')] })
        console.log(todaymoment)
      }, 5000)
  })

logs:

04/01/2022, 03:30:23am
04/01/2022, 03:30:23am
04/01/2022, 03:30:23am
04/01/2022, 03:30:23am

I also tried different library, but results are same.

CodePudding user response:

This could be because you have initialised the var todayMoment once and you are just printing it again and again every timeout, you are not really getting a new time value every 5 seconds.

You could place your var todaymoment = moment().format('L, hh:mm:ssa'); inside the setTimeout

To explain this you can see the jsFiddle here - https://jsfiddle.net/kjzq567h/4/

  • Related