javascript - 为什么 setInterval 在使用 Date 方法时返回相同的值
问题描述
我正在尝试从中获取本地秒数,getSeconds
但是当我console.log
通过在秒数中添加函数的结果setInterval
是相同的但它们正在增加时。请在下面找到我的代码;
const time = new Date();
function clock() {
const seconds = time.getSeconds();
console.log(seconds)
}
let interval = setInterval('clock()', 1000);
解决方案
您正在为间隔函数之外的时间(并且只有一次)分配值,因此它总是相同的。尝试:
function clock() {
let time = new Date();
const seconds = time.getSeconds();
console.log(seconds)
}
let interval = setInterval('clock()', 1000);
推荐阅读
- r - 数据为 POSIXct 时,ggplotly() 不显示 geom_vline / geom_hline
- ruby-on-rails - 为什么我使用的任何邮件程序都会打开我的默认文本编辑器而不是另一个浏览器选项卡?
- sql - 在 SQL 中使用 Sysdate 查找员工年龄
- javascript - 平均堆栈应用程序 user.comparePassword 不起作用
- angular - ng-sidebar 在嵌套模块的组件中不起作用
- ios - GMSMarker infoWindow 未更新
- javascript - 警告 ESLint 规则下的意外未命名函数 func-names
- react-native - undefined 不是函数(评估导航(屏幕))
- types - 如何在 Kotlin 中声明具有两种类型的变量,例如 val x: Int 或 String
- json - 如何分别提取 JSON 路径?