如何自动更改倒计时

时间:2016-06-16 11:44:09

标签: javascript jquery html

我有一个月的倒计时脚本,所以当倒计时到零时我怎么办,那么它应该自动更改到下个月的新倒计时日期?没有我自己每个月手动更改它。

其javascript

<script type="text/javascript">
    jQuery(document).ready( function($){
        var newDate = new Date(2016, 4, 21);
        newDate.setHours(newDate.getHours() + 18);
        $('#countdown-ex1').countdown({until: newDate});
    });
</script>

和Html

<div id="countdown-ex1" class="countdown countdown-large coming-soon divcenter bottommargin" style="max-width:700px;"></div>

3 个答案:

答案 0 :(得分:1)

与您的问题相关的唯一代码行是设置newDate变量的位置:

var newDate = new Date(2016, 4, 21);

应该在功能之外设置。然后,当函数过期时,您可以设置新值并再次运行该函数。此外,其中的数字也应该是变量:

var yearIs=2016;
var monthIs=4;
var dayIs=12;
newDate = new Date(yearIs, monthIs, dayIs);

答案 1 :(得分:1)

使用此示例,您的某月中的某一天将是静态的,但月份将会增加以确保将来array = ["1","2","1","2"] $.uniqueSort(array) => ["1", "2"]

您可以根据需要设置newDate日期 - 过去不要将其设置得太远以保存浏览器一些迭代次数。

startDate
jQuery(document).ready(function($) {
  var now = new Date();
  var startDate = new Date(2016, 3, 20);
  var newDate = new Date(startDate);
  while (newDate <= now) {
    // this will increment the date by 1 month until we're in the future
    newDate.setMonth(newDate.getMonth() + 1);
  }
  newDate.setHours(newDate.getHours() + 18);
  console.log(newDate);
  // the following check is only so we can run the code on SO without errors
  if ($('#countdown-ex1').length) {
    $('#countdown-ex1').countdown({
      until: newDate,
      expiryUrl: window.location // your URL here, to refresh the page
    });
  }
});

对于在计数器达到零之前有人保持页面打开的不太可能的事件,您可以设置<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>,以便倒计时将刷新您的页面。

答案 2 :(得分:1)

所以你应该在DB中输入所需的日期。在模块中创建一个获取下一个日期的函数,将其发送到控制器,在控制器中你应该有这样的东西:

    $this->load->model('event_model');      
    $next = $this->event_model->getNext()[0]->next_date;    
    $next_date = explode("/",$next);

    $year = $next_date[0];
    $month = $next_date[1];
    $day = $next_date[2];

    $data['date_year'] = $year;     
    $data['date_month'] = $month;       
    $data['date_day'] = $day;   

在您的HTML中,您应该输入:

    var newDate = new Date(<?php echo $date_year; ?>, <?php echo $date_month; ?>, <?php echo $date_day; ?>);

管理日期,您可以在管理员或其他任何地方手动删除,或制作某种检查实际日期/时间的脚本,并删除日期传递的记录。

多数民众赞成,希望有所帮助