假设我的前端代码中有 2 个标签,每个标签代表一个计时器,并且它们必须每秒更新一次。为此,我创建了一个 javascript 函数来完成这项工作,它接收实际的 DateTime 和标签 ID。
现在,我想将函数注册到 setInterval 方法两次,每次都有自己的参数。知道为什么以下代码不起作用吗?
(function () {
function updateTimerLabel(label, currentDateTime) {
currentDateTime.setSeconds(currentDateTime.getSeconds() + 1);
label.text(currentDateTime.getHours().toString() + ":" + currentDateTime.getMinutes().toString() + ":" + currentDateTime.getSeconds().toString());
}
;
setInterval(function () { updateTimerLabel(label1, dateTime1); }, 1000);
setInterval(function () { updateTimerLabel(label2, dateTime2); }, 1000);
}())