【问题标题】:JavaScript countdown from php timestamp difference来自php时间戳差异的JavaScript倒计时
【发布时间】:2012-03-13 15:10:23
【问题描述】:

我想倒计时时间,这是两个时间与php之间的差异,结果是时间戳。

{var $time = new \DateTime()}        

<div class="date" data-date="{= ($time2->getTimestamp() - $time->getTimestamp())*1000}">

在数据日期中,我有时间差异 [时间戳]。现在我想倒计时这一次。我从 HTML 到 JS 获取这些信息。

$(function() {        
    $(".date").each(function(){         
        time = $(this).data('date');        
        $.countdown($(this).children(".countdown"), time);
    });
}); 

有代码不能正常工作。

jQuery.countdown = function(selector, datevalue) {

                    var amount = datevalue;

        // catch past dates
        if(amount < 0){
            $(selector).html("Done");
        }

        // date is in the future, calculate the diff
        else{
            days=0;hours=0;mins=0;secs=0;out="";

            amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

            days=Math.floor(amount/86400);//days
            amount=amount%86400;

            hours=Math.floor(amount/3600);//hours
            amount=amount%3600;

            mins=Math.floor(amount/60);//minutes
            amount=amount%60;

            secs=Math.floor(amount);//seconds

            //if(days != 0){out += days +" day"+((days!=1)?"s":"")+", ";}
                            //if(days == 0) {
                                if(days != 0 || hours != 0){out += ((hours<10)?"0":"") + hours +":";}
                                if(days != 0 || hours != 0 || mins != 0){out += ((mins<10)?"0":"") + mins +":";}
                                out += ((secs<10)?"0":"") + secs;
                                $(selector).html(out);
                            //}
            // run it all again
            setTimeout(function() {  
                $.countdown(selector, datevalue);
            }, 1000); 

        }

};

来自 JS 的时间在正确的地方,但它没有倒计时。

【问题讨论】:

  • 如果您将超时时间设置为 1 秒,它应该如何工作?
  • time = parseInt($(this).data('date'),10); 也许?
  • 如果你删除amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs,你可以从你的php中删除*1000

标签: php javascript jquery timestamp countdown


【解决方案1】:

答案很简单:你不要减少 datevalue 变量。所以所有迭代都是一样的

看看下面的例子,效果很好

jQuery.countdown = function(selector, datevalue) {

    var amount = datevalue;

    // catch past dates
    if(amount < 0){
        $(selector).html("Done");
    }

    // date is in the future, calculate the diff
    else{
        datevalue--;
        $(selector).html(datevalue);
        setTimeout(function() {  
            $.countdown(selector, datevalue);
        }, 1000); 
    }
};

$.countdown('.date', 10);​​​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2014-05-04
    • 2012-06-03
    相关资源
    最近更新 更多