MOMT.JS没有更新时间,仅显示启动.js文件的时间
我试图在特定消息中包含实际日期和时间,但是我无法做到这一点,因为 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
我也尝试了不同的库,但结果是相同的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您已经初始化了 var TodayMoment 一次,并且只是在每次超时时一次又一次地打印它,您并没有真正每 5 秒获得一个新的时间值。
您可以将
var Todaymoment = moment().format('L, hh:mm:ssa');
放在 setTimeout 内要解释这一点,您可以在此处查看 jsFiddle - https://jsfiddle.net/kjzq567h/4/
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 setTimeoutTo explain this you can see the jsFiddle here - https://jsfiddle.net/kjzq567h/4/