Javascript倒计时在指定日期显示/隐藏&小时

时间:2016-12-02 13:15:37

标签: javascript jquery html

您好我一直在尝试使用一些我可以部分工作的代码,我想要一个倒计时,我们可以设置一个倒计时的结束时间(很明显很明显),我们也想要将其设置为仅在一天中的某些时间显示,而且仅在一周中的某些日期显示。

我已经成功完成了以下工作,因此我们可以设置一天的时间来展示,但我无法让它工作,所以它只显示在特定的指定日期。有人可以帮忙吗?

var countdownMessage = "This ends in";
var now = new Date();
var time = now.getTime(); // time now in milliseconds
var countdownEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 12, 59); // countdownEnd 0000 hrs

//////////////////////////* Countdown *///////////////////////////////
function getSeconds() {
  var ft = countdownEnd.getTime() + 86400000; // add one day
  var diff = ft - time;
  diff = parseInt(diff / 1000);
  if (diff > 86400) {
    diff = diff - 86400
  }
  startTimer(diff);
}

var timeInSecs;
var ticker;

function startTimer(secs) {
  timeInSecs = parseInt(secs);
  ticker = setInterval("tick()", 1000);
  tick(); // to start counter display right away
}

function tick() {
  var secs = timeInSecs;
  if (secs > 0) {
    timeInSecs--;
  } else {
    clearInterval(ticker); // stop counting at zero
    //getSeconds();  // and start again if required
  }

  var hours = Math.floor(secs / 3600);
  secs %= 3600;
  var mins = Math.floor(secs / 60);
  secs %= 60;
  var result = ((hours < 10) ? "0" : "") + hours + " hours " + ((mins < 10) ? "0" : "") + mins + " minutes " + ((secs < 10) ? "0" : "") + secs + " seconds";
  document.getElementById("countdown").innerHTML = (countdownMessage) + " " + result;
}


///////////////* Display at certain time of the day *//////////////////

//gets the current time. 
var d = new Date();
if (d.getHours() >= 7 && d.getHours() <= 15) {
  $("#countdown").show();
} else {
  $("#countdown").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="getSeconds()">

  <span id="countdown" style="font-weight: bold;"></span>

</body>

[编辑]

为了补充一点,我尝试将脚本的一部分更改为此但不起作用:

$(function() {
$("#countdown").datepicker(
    { beforeShowDay: function(day) {
        var day = day.getDay();
        if (day == 1 || day == 2) {
            //gets the current time. 
            var d = new Date();
            if(d.getHours() >= 7 && d.getHours() <= 10 ){
                $("#countdown").show();
            }
            else {  
                 $("#countdown").hide();
            }
        } else {
            $("#countdown").hide();
        }
    }
});

});

1 个答案:

答案 0 :(得分:2)

除了你将字符串值传递为setInterval而不是函数引用setInterval("tick()", 1000)

setInterval(tick, 1000)部分外,你所做的一切都很好

此外,我已更新以下代码,以检查特定日期以及您的具体时间

var d = new Date();
var day = d.getDay();

if (day == 0 || day == 6) {
  if (d.getHours() >= 0 && d.getHours() <= 8) {
    $("#countdown").show();
  } else {
    $("#countdown").hide();
  }
}

您可以在下面试一试,

&#13;
&#13;
var countdownMessage = "This ends in";
var now = new Date();
var time = now.getTime(); // time now in milliseconds
var countdownEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 12, 59); // countdownEnd 0000 hrs

//////////////////////////* Countdown *///////////////////////////////
function getSeconds() {
  var ft = countdownEnd.getTime() + 86400000; // add one day
  var diff = ft - time;
  diff = parseInt(diff / 1000);
  if (diff > 86400) {
    diff = diff - 86400
  }
  startTimer(diff);
}

var timeInSecs;
var ticker;

function startTimer(secs) {
  timeInSecs = parseInt(secs);
  ticker = setInterval(tick, 1000);
  tick(); // to start counter display right away
}

function tick() {
  var secs = timeInSecs;
  if (secs > 0) {
    timeInSecs--;
  } else {
    clearInterval(ticker); // stop counting at zero
    //getSeconds();  // and start again if required
  }

  var hours = Math.floor(secs / 3600);
  secs %= 3600;
  var mins = Math.floor(secs / 60);
  secs %= 60;
  var result = ((hours < 10) ? "0" : "") + hours + " hours " + ((mins < 10) ? "0" : "") + mins + " minutes " + ((secs < 10) ? "0" : "") + secs + " seconds";
  document.getElementById("countdown").innerHTML = (countdownMessage) + " " + result;
}

$("#countdown").hide();

///////////////* Display at certain time of the day *//////////////////

//gets the current time. 
var d = new Date();
var day = d.getDay();

if (day == 0 || day == 6) {
  if (d.getHours() >= 0 && d.getHours() <= 8) {
    $("#countdown").show();
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="getSeconds()">

  <span id="countdown" style="font-weight: bold;"></span>

</body>
&#13;
&#13;
&#13;