JavaScript 'countdown' 不是定义错误
javascript 'countdown' is not defined error
我不断得到"倒计时未定义"这一行
var timeout = setTimeout('countdown()',1000);
它应该每秒循环一次function countdown()
。 我该如何解决这个问题?
function countdown() {
var until = $('.time-elapsed').attr('data-time');
var nextmonth = new Date(until);
var now = new Date();
var timeDiff = nextmonth.getTime() - now.getTime();
if(timeDiff <=0) {
var nextmonth = new Date(until);
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours%=24;
minutes%=60;
seconds%=60;
$('.time-elapsed').find('ul:eq(0)').find('li:eq(1)').html(days);
$('.time-elapsed').find('ul:eq(1)').find('li:eq(1)').html(hours);
$('.time-elapsed').find('ul:eq(2)').find('li:eq(1)').html(minutes);
$('.time-elapsed').find('ul:eq(3)').find('li:eq(1)').html(seconds);
var timeout = setTimeout('countdown()',1000);
}
不要使用字符串作为 setTimeout 的第一个参数。使用函数:
var timeout = setTimeout(countdown,1000);
我建议您使用setInterval(countdown,1000)
并删除函数底部的setTimeout(countdown,1000)
。 setInterval
将无限重复countdown
函数。
要阻止它,只需使用clearInterval(timeout);
相关文章:
- chrome.storage.sync未定义错误
- 为什么foodModule没有定义错误?Angular js
- 对象为null或IE9中存在未定义错误
- 是否排除节点中错误堆栈的第一行?/节点中的自定义错误类型
- axios promise如何定义错误
- 已定义变量出现未定义错误
- 'define' 在 RequireJS 和 Webapp Yo 生成器上未定义错误
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- onreadystatechange未定义错误
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 登录时显示自定义错误消息
- 如何修复JSlint报告的“foo未定义”错误
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 主干无法读取属性'属性'主干视图中的未定义错误
- 如何克服javascript中的未定义错误
- ReferenceError:事件未在Firefox中定义错误
- 当我在异步操作Redux上开始单元测试时,没有定义错误承诺
- require未定义错误
- AJAX自定义错误处理代码问题
- 单击链接时出现函数未定义错误