Javascript 修改倒计时脚本以每周重复特定的日期和时间

Javascript 修改倒计时脚本以每周重复特定的日期和时间,javascript,jquery,Javascript,Jquery,我在浏览不同的倒计时脚本想法。我喜欢这个方法,因为你可以输入一个精确的日期,而不必为数字日期减去1,因为它们从0开始 function cdtd() { var xmas = new Date(" 00:01:00"); var now = new Date(); var timeDiff = xmas.getTime() - now.getTime(); if (timeDiff <= 0) { clearTimeout(t

我在浏览不同的倒计时脚本想法。我喜欢这个方法,因为你可以输入一个精确的日期,而不必为数字日期减去1,因为它们从0开始

function cdtd() {
    var xmas = new Date(" 00:01:00");
    var now = new Date();
    var timeDiff = xmas.getTime() - now.getTime();
    if (timeDiff <= 0) {
                clearTimeout(timer);
        document.write("Christmas is here!");
        // Run any code needed for countdown completion here
        }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
        minutes %= 60;
        seconds %= 60;
    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;
    var timer = setTimeout('cdtd()',1000);
}
HTML

运行函数

<script type="text/javascript">cdtd();</script>
我需要它在这样一个函数中;然而,我现在有一个项目,需要每周重复倒计时,以检测一周中的特定日期和时间

例如:

星期日上午10:00

它需要倒计时,直到达到那个点。周日上午10:00之后,需要重新开始并倒计时,直到下一个周日上午10:00

我怎样才能做到这一点。同样,我喜欢倒计时示例的整体结构;然而,如果你有一个更好的脚本倒计时计时器,我愿意听取其他人的意见


谢谢

像这样的方法会奏效:

但是,我现在会尝试通过PHP将var替换为服务器时间,这样可以确保所有用户都看到相同的倒计时

 function plural(s, i) {
      return i + ' ' + (i > 1 ? s + 's' : s);
    }

function sundayDelta(offset) {
  // offset is in hours, so convert to miliseconds
  offset = offset ? offset * 60 * 60 * 1000 : 0;
  var now = new Date(new Date().getTime() + offset);
  var days = 7 - now.getDay() || 7;
  var hours = 24 - now.getHours();
  var minutes = 60 - now.getMinutes();
  var seconds = 60 - now.getSeconds();
  return [plural('day', days),
          plural('hour', hours),
          plural('minute', minutes), 
          plural('second', seconds)].join(' ');
}

// Save reference to the DIV
$refresh = $('#refresh');

$refresh.text('This page will refresh in ' + sundayDelta());

// Update DIV contents every second
setInterval(function() {
  $refresh.text('This page will refresh in ' + sundayDelta());
}, 1000);

您可能应该在服务器端执行此操作。客户端倒计时将取决于用户的时间。因此,每个用户可能会看到不同的时间/倒计时。您可以添加一个代码示例来说明如何在服务器端执行此操作吗?老实说,我甚至不知道该怎么做。我确实读过关于在服务器端做这件事,我想。你用什么语言?我可以使用PHP和Javascript。你有倒计时代码。现在,您只需要编写一个代码来检查您输入的下一个星期是什么时候。比如,如果今天是4月15日星期三,而您的输入是SUN,您将很容易地计算出日期是4月19日。然后将该日期放入倒计时代码中。在寻求帮助之前,请自己尝试一下。如果你有时间,你能告诉我如何使用PHP和Javascript设置服务器时间吗?不是一个真正的PHP专家,但这看起来应该涵盖它。。。。如何在页面上显示倒计时?我添加了你的代码;然而,我不知道该怎么称呼它。但是感谢您在php服务器上的输入time.awesome!忘记添加$document.readyfunction{在javascript.Works now周围。此代码是否占周日的特定时间。周日上午10:00?抱歉,正在研究如何阅读您编写的代码。刚刚看到它工作了。对javascript来说是相当新的。
 function plural(s, i) {
      return i + ' ' + (i > 1 ? s + 's' : s);
    }

function sundayDelta(offset) {
  // offset is in hours, so convert to miliseconds
  offset = offset ? offset * 60 * 60 * 1000 : 0;
  var now = new Date(new Date().getTime() + offset);
  var days = 7 - now.getDay() || 7;
  var hours = 24 - now.getHours();
  var minutes = 60 - now.getMinutes();
  var seconds = 60 - now.getSeconds();
  return [plural('day', days),
          plural('hour', hours),
          plural('minute', minutes), 
          plural('second', seconds)].join(' ');
}

// Save reference to the DIV
$refresh = $('#refresh');

$refresh.text('This page will refresh in ' + sundayDelta());

// Update DIV contents every second
setInterval(function() {
  $refresh.text('This page will refresh in ' + sundayDelta());
}, 1000);