Javascript倒计时&设置超时错误

发布于 2024-11-10 01:54:02 字数 1273 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫多说 2024-11-17 01:54:02

您不能在字符串中传递 countdownDiv 或 endDate。 Replace:

setTimeout("initCountdown('"+ countdownDiv +"', '"+ endDate +"', '"+ endMsg +"' )", 1000);

with:

setTimeout(function(){initCountdown(countdownDiv, endDate, endMsg); countdownDiv = null; endDate = null; endMsg = null}, 1000);

设置为 null 是修复某些浏览器中不良垃圾收集的肮脏技巧。

You cannot pass countdownDiv or endDate in a string. Replace:

setTimeout("initCountdown('"+ countdownDiv +"', '"+ endDate +"', '"+ endMsg +"' )", 1000);

with:

setTimeout(function(){initCountdown(countdownDiv, endDate, endMsg); countdownDiv = null; endDate = null; endMsg = null}, 1000);

Setting to null is a dirty trick to fix for bad garbage collection in some browsers.

我偏爱纯白色 2024-11-17 01:54:02

setTimeout 采用函数作为第一个参数。删除前导引号和尾随引号

setTimeout takes a function as the first parameter. Remove the leading and trailing quotes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文