Javascript倒计时&设置超时错误
function initCountdown(countdownDiv, endDate, endMsg) {
current_date = new Date();
time_left = endDate.getTime() - current_date.getTime();
if(time_left>0) {
time_left = Math.floor(time_left/1000);
days=0;hours=0;mins=0;secs=0;out="";
days=Math.floor(time_left/86400);
time_left=time_left%86400;
hours=Math.floor(time_left/3600);
time_left=time_left%3600;
mins=Math.floor(time_left/60);
time_left=time_left%60;
secs=Math.floor(time_left);
var daysTxt = "<strong>"+days +"</strong>day"+((days!=1)?"s":"");
var hoursTxt = "<strong>"+hours +"</strong>hour"+((hours!=1)?"s":"");
var minTxt = "<strong>"+mins +"</strong>min"+((mins!=1)?"s":"");
var secTxt = "<strong>"+secs +"</strong>sec";
var finalTxt = daysTxt + " : " + hoursTxt+ " : " + minTxt + " : "+ secTxt;
$(countdownDiv).html(finalTxt)
setTimeout("initCountdown('"+ countdownDiv +"', '"+ endDate +"', '"+ endMsg +"' )", 1000);
}else{
$(countdownDiv).html(endMsg)
}
}
除了 setTimeout
行之外,似乎所有工作都有效,可能我在调用该函数时犯了一个错误。
谁能告诉我错误在哪里?
谢谢
function initCountdown(countdownDiv, endDate, endMsg) {
current_date = new Date();
time_left = endDate.getTime() - current_date.getTime();
if(time_left>0) {
time_left = Math.floor(time_left/1000);
days=0;hours=0;mins=0;secs=0;out="";
days=Math.floor(time_left/86400);
time_left=time_left%86400;
hours=Math.floor(time_left/3600);
time_left=time_left%3600;
mins=Math.floor(time_left/60);
time_left=time_left%60;
secs=Math.floor(time_left);
var daysTxt = "<strong>"+days +"</strong>day"+((days!=1)?"s":"");
var hoursTxt = "<strong>"+hours +"</strong>hour"+((hours!=1)?"s":"");
var minTxt = "<strong>"+mins +"</strong>min"+((mins!=1)?"s":"");
var secTxt = "<strong>"+secs +"</strong>sec";
var finalTxt = daysTxt + " : " + hoursTxt+ " : " + minTxt + " : "+ secTxt;
$(countdownDiv).html(finalTxt)
setTimeout("initCountdown('"+ countdownDiv +"', '"+ endDate +"', '"+ endMsg +"' )", 1000);
}else{
$(countdownDiv).html(endMsg)
}
}
seem all works except the line with setTimeout
probably I make a mistake on recall of the function.
Can anyone tell me where is the mistake?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在字符串中传递 countdownDiv 或 endDate。 Replace:
with:
设置为 null 是修复某些浏览器中不良垃圾收集的肮脏技巧。
You cannot pass countdownDiv or endDate in a string. Replace:
with:
Setting to null is a dirty trick to fix for bad garbage collection in some browsers.
setTimeout 采用函数作为第一个参数。删除前导引号和尾随引号
setTimeout takes a function as the first parameter. Remove the leading and trailing quotes