更新日期对象为现在()

发布于 2025-02-10 19:36:36 字数 732 浏览 1 评论 0 原文

我需要以一个秒的间隔阅读当前时间。
我是否应该依靠垃圾收集来照顾 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

I need to read the current time in one second intervals.
Should I rely on garbage collection to take care of regularly created instances of Date like mytime = new Date() or should I update mytime with the static Date.now() - and how?


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-02-17 19:36:36

除非出于某种原因,您需要确保您只有一个共享实例并且它被突变(提示:您真的不应该),只需在需要时创建新的日期对象即可。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文