2015-11-14 95 views
0

我目前有一个倒数计时器,显示小时,分钟,秒。但是现在我想给它添加“天”。你能告诉我你将如何做到这一点?需要帮助添加“天”到这个jQuery的倒数计时器

这是代码。我遗漏了php分贝查询,因为它不重要。

function Timer(container, timeLeft) { 
 
    // get hour, minute and second element using jQuery selector 
 
    var hoursContainer = $(container).find('.hour'); 
 
    var minsContainer = $(container).find('.min'); 
 
    var secsContainer = $(container).find('.sec'); 
 
    
 
    // hold time left 
 
    var currentTimeLeft = timeLeft; 
 
    // 1 second = 1000 ms 
 
    var secondsForTimer = 1000; 
 
    // hold ID value return by setInterval() 
 
    var timerInterval; 
 
    
 
    // call setInteval() only when timeLeft is greater than 0 
 
    if (currentTimeLeft == 0) { 
 
\t return; 
 
    } else { 
 
\t //Call setInterval()function and store ID value to timerInterval. 
 
\t timerInterval = setInterval(countdown, secondsForTimer); 
 
    } 
 
    
 
    //function being passed to setInterval() 
 
    function countdown() { 
 
    currentTimeLeft = parseInt(currentTimeLeft - secondsForTimer);  
 
    if (currentTimeLeft == 0) { 
 
     //stop calling countdown function by calling clearInterval() 
 
     clearInterval(timerInterval); 
 
     return; 
 
    } else {  \t 
 
     //calculate hours left 
 
     var wholeSeconds = parseInt(currentTimeLeft/1000,10); 
 
     var wholeMinutes = parseInt(currentTimeLeft/60000,10); 
 
     var wholeHours = parseInt(wholeMinutes/60,10); 
 
     //calculate minutes left 
 
     var minutes = parseInt(wholeMinutes % 60,10); 
 
     //calculate seconds left 
 
     var seconds = parseInt(wholeSeconds % 60,10); 
 
     //prefix 0 to hour, min and second counter 
 
     $(hoursContainer).text((wholeHours < 10 ? "0" : "") + wholeHours + (wholeHours <=0 ? " hr" : " hrs")); 
 
     $(minsContainer).text((minutes < 10 ? "0" : "") + minutes + (minutes <=0 ? " min" : " mins")); 
 
     $(secsContainer).text((seconds < 10 ? "0" : "") + seconds + (seconds <=0 ? " sec" : " secs")); 
 
    } 
 
    } 
 
}
<?php 
 

 
// db query here to get the expiry time from the database 
 
foreach($results as $k => $row) { 
 

 
    $expiry_date \t \t = \t $row['expiry_date']; 
 

 
    $timeLeft = (strtotime($expiry_date) - time()) * 1000; 
 
    $counterName = "counter_$k"; 
 

 
    ?> 
 
    <div class="counter <?php echo $counterName; ?>"> \t 
 
    <span class="hour">00</span> 
 
    <span class="min">00</span> 
 
    <span class="sec">00</span> 
 
    </div> 
 

 
    <script> 
 

 
    // initiate new timer 
 
    var timer = new Timer($('.<?php echo $counterName; ?>'), <?php echo $timeLeft; ?>); 
 

 
    </script> 
 

 
<?php 
 
} 
 

 
?>

回答

0

试试这个

function Timer(container, timeLeft) { 
    // get hour, minute and second element using jQuery selector 
    var daysContainer = $(container).find('.day'); 
    var hoursContainer = $(container).find('.hour'); 
    var minsContainer = $(container).find('.min'); 
    var secsContainer = $(container).find('.sec'); 

    // hold time left 
    var currentTimeLeft = timeLeft; 
    // 1 second = 1000 ms 
    var secondsForTimer = 1000; 
    // hold ID value return by setInterval() 
    var timerInterval; 

    // call setInteval() only when timeLeft is greater than 0 
    if (currentTimeLeft == 0) { 
     return; 
    } else { 
     //Call setInterval()function and store ID value to timerInterval. 
     timerInterval = setInterval(countdown, secondsForTimer); 
    } 

    //function being passed to setInterval() 
    function countdown() { 
    currentTimeLeft = parseInt(currentTimeLeft - secondsForTimer);  
    if (currentTimeLeft == 0) { 
     //stop calling countdown function by calling clearInterval() 
     clearInterval(timerInterval); 
     return; 
    } else {   
     //calculate hours left 
     var wholeSeconds = parseInt(currentTimeLeft/1000,10); 
     var wholeMinutes = parseInt(currentTimeLeft/60000,10); 
     var wholeHours = parseInt(wholeMinutes/60,10); 
     var wholeDays = parseInt(wholeHours/24,10); 
     //calculate hours left 
     var hours = parseInt(wholeHours % 24,10); 
     //calculate minutes left 
     var minutes = parseInt(wholeMinutes % 60,10); 
     //calculate seconds left 
     var seconds = parseInt(wholeSeconds % 60,10); 
     //prefix 0 to hour, min and second counter 
     $(daysContainer).text((wholeDays < 10 ? "0" : "") + wholeDays + (wholeDays <=0 ? " day" : " days")); 
     $(hoursContainer).text((hours < 10 ? "0" : "") + hours + (hours <=0 ? " hr" : " hrs")); 
     $(minsContainer).text((minutes < 10 ? "0" : "") + minutes + (minutes <=0 ? " min" : " mins")); 
     $(secsContainer).text((seconds < 10 ? "0" : "") + seconds + (seconds <=0 ? " sec" : " secs")); 
    } 
    } 
} 

加入天容器上你的循环

<div class="counter <?php echo $counterName; ?>"> 
    <span class="day">00</span> 
    <span class="hour">00</span> 
    <span class="min">00</span> 
    <span class="sec">00</span> 
</div> 

小提琴:https://jsfiddle.net/otezz/68d9yb6v/1/

+0

我做到了。它确实显示了日子,但它也显示了总时数。例如像这样。 **原始代码显示:** 48小时5分钟10秒 **您的代码显示**:2天48小时5分钟10秒 – smith

+0

我的代码工作得很好。 https://jsfiddle.net/otezz/68d9yb6v/1/ – otezz

+0

啊,是的,我错过了你改变的一行。很棒。谢谢。现在我有几个新问题。 1.如果他们是0,我怎么能不显示“天”? 2.当它倒数到零时,实际上停在1秒。我怎样才能让它计数到0秒,然后在那之后显示“过期”之类的内容? – smith