javascript - 使 JavaScript 间隔与实际时间同步

标签 javascript time

我想让秒数与实时 (new Date()) 秒数同步,以便在页面加载到中点时它也能正常工作。

setInterval(() => {
    console.log("hey im not synchronized");
}, 1000);

这可能吗?

最佳答案

Is that possible?

是的,像这样:

setTimeout(() => {
    setInterval(() => console.log("hey im synchronized"), 1000)
}, 1000 - new Date().getMilliseconds());

关于javascript - 使 JavaScript 间隔与实际时间同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53891790/

相关文章:

javascript - 使用 jest 测试提交以获取的 FormData

javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin 'http://localhost:8100'

javascript - 使用 http-proxy 代理对新端口的请求

c++ - gmtime_r 和 gmtime_s 的区别

php - 在mysql中为日期和时间选择数据类型

python - 如何轻松操作 Python 字典中的时间戳键?

javascript - 将 React 与 Cloud Firestore 结合使用

javascript - Promise rejection 抛出警告,即使它稍后被捕获

c - 定时服务器响应时间