【发布时间】:2017-10-10 08:44:42
【问题描述】:
所以我试图编写一些javascript来倒计时,然后在倒计时完成时提交表单。这是我在网上找到并尝试实现的JS:
var seconds = 300;
function secondPassed() {
var minutes = Math.round((seconds - 30) / 60),
remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
document.qForm.submit();
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
这里是这样实现的: https://jsfiddle.net/spadez/9p9o4k6s/1/
错误提示:secondPassed 未定义,但我不确定为什么会这样。谁能解释我哪里出错了?
【问题讨论】:
-
改成
setInterval(secondPassed, 1000);详情请见setInterval Documentation -
我知道How do I correctly use setInterval and clearInterval to switch between two different functions? 的这个可能重复项要求的不仅仅是清除和设置间隔,而是通过不将函数引用指定为字符串来显示如何正确使用它。
标签: javascript