更新日期对象为现在()
我需要以一个秒的间隔阅读当前时间。
我是否应该依靠垃圾收集来照顾 date 喜欢 mytime = new Date()
,或者我应该使用静态 date.now()
- 以及如何?
Put differently:
let mytime = new Date();
...
// regularly
// A)
mytime = new Date(); // replace with current instance, rely on garbage collection
// OR
// B)
mytime.setUTCMilliseconds(Date.now()); // update original instance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非出于某种原因,您需要确保您只有一个共享实例并且它被突变(提示:您真的不应该),只需在需要时创建新的日期对象即可。 GC会照顾好旧的。
Unless for some reason you need to make sure that you have only a single shared instance and that it gets mutated (hint: you really shouldn't), just create new date objects whenever you need them. GC will take care of the old ones just fine.