moment.js 不更新时间,只显示 .js 文件启动的时间

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

我正在尝试将实际日期和时间包含在特定消息中,但我做不到,因为 moment.js 一直卡在同一时间。出于某种原因,它只会在脚本启动时一遍又一遍地触发相同的时间。

//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)
  })

日志:

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

我也尝试了不同的库,但结果是一样的。

这可能是因为您已经初始化了一次 var todayMoment 并且您只是在每次超时时一次又一次地打印它,您并没有真正每 5 秒获得一个新的时间值。

您可以将 var todaymoment = moment().format('L, hh:mm:ssa'); 放在 setTimeout

为了解释这一点,您可以在此处查看 jsFiddle - https://jsfiddle.net/kjzq567h/4/