2014-10-27 53 views
-1

這是我第一次在這裏問一個問題,所以我希望我做對了。事件的jQuery倒數計時器

我正在爲我們食堂的營業時間倒計時。 這個食堂開放時間爲7.45至12.45,然後休息至13.30。

我已經倒計時到了7.45,但是我希望在12.45休息時重置倒計時到13.30。 週五他們收於12.45,所以當天沒有休息。

這是到目前爲止我的代碼:

的index.php

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>countdown Timer</title> 
    <meta charset="utf-8"> 
</head> 
<body> 
    <div id="countdown"> 
     <span class="days">00</span> Days 
     <span class="hours">00</span> Hours 
     <span class="mins">00</span> Minutes 
     <span class="secs">00</span> Secounds 
    </div> 

    <script type="text/javascript" src="js/jquery.min.js"></script> 
    <script type="text/javascript" src="js/countdown.jquery.js"></script> 
    <script type="text/javascript" src="js/ext.js"></script> 
</body> 
</html> 

ext.js

$(document).ready(function() { 
    $('#countdown').countdown({ date: '28 october 2014 7:45:00' }, function() { 
     $('#countdown').text('The canteen is open :D') 
    }); 
}); 

countdown.jquery.js

(function($) { 
    $.fn.countdown = function(options, callback) { 
     var settings = { 'date': null }; 
     if (options) { 
      $.extend(settings, options); 
     } 

     this_sel = $(this); 

     function count_exec(){ 
      eventDate = Date.parse(settings['date'])/1000; 
      currentDate = Math.floor($.now()/1000) 

      if (eventDate <= currentDate) { 
       callback.call(this); 
       clearInterval(interval); 
      } 

      seconds = eventDate - currentDate; 

      days = Math.floor(seconds/(60*60*24)); 
      seconds -= days * 60 * 60 * 24; 

      hours = Math.floor(seconds/(60*60)); 
      seconds -= hours * 60 * 60; 

      minutes = Math.floor(seconds/60); 
      seconds -= minutes * 60; 

      days = (String(days).length <= 1) ? '0' + days : days; 
      hours = (String(hours).length <= 1) ? '0' + hours : hours; 
      minutes = (String(minutes).length <= 1) ? '0' + minutes : minutes; 
      seconds = (String(seconds).length <= 1) ? '0' + seconds : seconds; 

      if (!isNaN(eventDate)) { 
       this_sel.find('.days').text(days); 
       this_sel.find('.hours').text(hours); 
       this_sel.find('.mins').text(minutes); 
       this_sel.find('.secs').text(seconds); 
      } else { 
       document.getElementById("countdown").innerHTML = "Sorry, the countdown is not available"; 
      } 
     } 

     count_exec(); 
     interval = setInterval(count_exec, 1000); 
    } 
})(jQuery); 

回答

1

你可以有3日,7:45,12:45和13:30。如果當前日期是< 7:45,則顯示倒計時開始。如果當前日期> 12:45顯示倒計時至13:30。