问题描述
|
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
的行以外的所有作品都可能在调用该函数时出错。
谁能告诉我错误在哪里?
谢谢
解决方法
您不能在字符串中传递countdownDiv或endDate。更换:
setTimeout(\"initCountdown(\'\"+ countdownDiv +\"\',\'\"+ endDate +\"\',\'\"+ endMsg +\"\' )\",1000);
与:
setTimeout(function(){initCountdown(countdownDiv,endDate,endMsg); countdownDiv = null; endDate = null; endMsg = null},1000);
设置为null是在某些浏览器中修复不良垃圾收集的肮脏技巧。
,setTimeout将函数作为第一个参数。删除前导和尾随引号