如何使用php和ajax设置此倒计时?

如何使用php和ajax设置此倒计时?,php,javascript,jquery,Php,Javascript,Jquery,我有一个永久的倒计时,我需要设置,我正在努力与如何正确地做到这一点 我正在使用一个名为coutdown的插件,可以在这里找到: 到目前为止,我的设置包括以下代码: $(document).ready(function() { $('#countdown_contain').countDown({ targetDate: { 'day': 23, 'month': 6, 'year':

我有一个永久的倒计时,我需要设置,我正在努力与如何正确地做到这一点

我正在使用一个名为coutdown的插件,可以在这里找到:

到目前为止,我的设置包括以下代码:

$(document).ready(function() {
    $('#countdown_contain').countDown({
        targetDate: {
            'day':      23,
            'month':    6,
            'year':     2013,
            'hour':     12,
            'min':      0,
            'sec':      0
        },
        omitWeeks: true
    });
});
因此,它正在做的是倒计时到2013年6月23日星期日。当它归零时,我需要它复位,然后在两周内开始倒计时到同一时间。所以基本上我需要一个两周的倒数计时器,周日中午

我的想法是,在每次加载页面时对php脚本进行ajax调用,以查看与下一个倒计时日期相比我们的位置,然后将响应填充到JS变量中,这些变量将成为插件使用的信息。也许这太过分了


如何构造php或javascript来实现我的目标?

我将创建一个javascript函数来计算下一个有效日期,并使用它来设定计数器时间,而不是ajax。这样,就没有额外的服务器交互

至于自动重置,请使用
onComplete
事件重新设置控件的种子


要动态构建targetDate结构,我将使用。请注意,我在工作中的“非吸烟者休息时间”将其编码为理论,因此这是未经测试的。请编辑,如果它不能100%工作,请不要否决

function getTargetDate() {
     // initialize the static stuff...
     var retval = { 'hour':12,'min':0,'sec':0 }
     // good news! using Moment.js in the USA, Sunday is considered first day of week
     // so all we have to do is calculate the correct week number...
     var currentWeekOfYear = moment().week(); 
     // assume that we're targeting odd weeks, so 
     //  if it's odd, add 2, if it's even, add 1
     var nextTargetWeek = currentWeekOfYear;
     if( currentWeekOfYear % 2 == 0) {
         nextTargetWeek += 1;
     } else {
         nextTargetWeek += 2;
     }

     var nextTarget = moment().week(nextTargetWeek);

     // moment retains the current day, so we need to set it to sunday.
     nextTarget = nextTarget.day(0);

     // now, enrich the return value
     retval.day = nextTarget.date();
     retval.month = nextTarget.month();
     retval.year = nextTarget.year();

     return retval;
}

我不使用ajax,而是创建一个javascript函数来计算下一个有效日期,并使用它来设定计数器时间。这样,就没有额外的服务器交互

至于自动重置,请使用
onComplete
事件重新设置控件的种子


要动态构建targetDate结构,我将使用。请注意,我在工作中的“非吸烟者休息时间”将其编码为理论,因此这是未经测试的。请编辑,如果它不能100%工作,请不要否决

function getTargetDate() {
     // initialize the static stuff...
     var retval = { 'hour':12,'min':0,'sec':0 }
     // good news! using Moment.js in the USA, Sunday is considered first day of week
     // so all we have to do is calculate the correct week number...
     var currentWeekOfYear = moment().week(); 
     // assume that we're targeting odd weeks, so 
     //  if it's odd, add 2, if it's even, add 1
     var nextTargetWeek = currentWeekOfYear;
     if( currentWeekOfYear % 2 == 0) {
         nextTargetWeek += 1;
     } else {
         nextTargetWeek += 2;
     }

     var nextTarget = moment().week(nextTargetWeek);

     // moment retains the current day, so we need to set it to sunday.
     nextTarget = nextTarget.day(0);

     // now, enrich the return value
     retval.day = nextTarget.date();
     retval.month = nextTarget.month();
     retval.year = nextTarget.year();

     return retval;
}

这不是一个确切的答案,但它应该把你放在正确的轨道上

您可以直接将变量回显到javascript文件中

$(document).ready(function() {
$('#countdown_contain').countDown({
    targetDate: {
        'day':      <?php echo(json_encode($day)); ?>,
        'month':    <?php echo(json_encode($month)); ?>,
        'year':     <?php echo(json_encode($year)); ?>,
        'hour':     <?php echo(json_encode($hour)); ?>,
        'min':      <?php echo(json_encode($min)); ?>,
        'sec':      <?php echo(json_encode($sec)); ?>
    },
    omitWeeks: true
});
});
$(文档).ready(函数(){
$('倒计时')。倒计时({
目标日期:{
“天”:,
“月”:,
“年”:,
“小时”:,
“min”:,
“秒”:
},
两周:对
});
});

在php脚本中,您可以使用计算下一个目标日。

这不是一个确切的答案,但它应该让您走上正确的轨道

您可以直接将变量回显到javascript文件中

$(document).ready(function() {
$('#countdown_contain').countDown({
    targetDate: {
        'day':      <?php echo(json_encode($day)); ?>,
        'month':    <?php echo(json_encode($month)); ?>,
        'year':     <?php echo(json_encode($year)); ?>,
        'hour':     <?php echo(json_encode($hour)); ?>,
        'min':      <?php echo(json_encode($min)); ?>,
        'sec':      <?php echo(json_encode($sec)); ?>
    },
    omitWeeks: true
});
});
$(文档).ready(函数(){
$('倒计时')。倒计时({
目标日期:{
“天”:,
“月”:,
“年”:,
“小时”:,
“min”:,
“秒”:
},
两周:对
});
});

在php脚本中,您可以使用计算下一个目标日。

您能否提供一些见解,作为通过JS计算下一个有效日的最佳方法?如果我完全用javascript解决这个问题,这是完全可行的,我会找到一个像样的javascript日期库。同意。我正在研究一些,看看现在的情况。jsYep这很好,真的为我指明了正确的方向,谢谢。你能提供一些见解,作为通过JS计算下一个有效日期的最佳方法吗?如果我完全用javascript解决这个问题,这是完全可行的,我会找到一个像样的javascript日期库。同意。我正在研究一些,现在正在看。是的,这很好用,真的为我指明了正确的方向,谢谢。