使用Javascript创建倒计时

时间:2014-01-31 20:06:17

标签: javascript html

我需要创建一个倒计时,每周一至周五上午8点至晚上8点,周六和周日上午8点至下午6点,然后当它完成时,就像“我们明天早上8点开门!”这样的话。

我怎样才能做到这一点?

网站:(你可以看到倒计时的位置。)

http://www.securemyhome.com/test-pulse3

HTML

 <div class="wereOpen"><span class="blue">We'll Call You!</span><br />Only 
 <span class="countdown">
 <!--  COUNTDOWN BANNER  -->
        7 hours 
        35 minutes 
</span> left!</div>

这是我试过的一些。

var count=30;

var counter=setInterval(timer, 1000); //1000 will  run it every 1 second

function timer()
{
count=count-1;
if (count <= 0)
{
 clearInterval(counter);
 //counter ended, do something here
 return;
 }

 //Do code for showing the number of seconds here
 }

2

 function Countdown(options) {
  var timer,
 instance = this,
   seconds = options.seconds || 10,
 updateStatus = options.onUpdateStatus || function () {},
 counterEnd = options.onCounterEnd || function () {};

 function decrementCounter() {
 updateStatus(seconds);
 if (seconds === 0) {
  counterEnd();
  instance.stop();
   }
   seconds--;
  }

  this.start = function () {
  clearInterval(timer);
   timer = 0;
  seconds = options.seconds;
  timer = setInterval(decrementCounter, 1000);
  };

  this.stop = function () {
   clearInterval(timer);
  };
 }

1 个答案:

答案 0 :(得分:2)

由于每个人都在撕扯你并且没有任何帮助,所以这里的代码与你想要的类似。相应调整。

// 24 hour based
var targetHour = 10;

var currentTime = new Date();

// sun 0 mon 1 ... fri 5 sat 6
var currentDay = currentTime.getDay();

var offset = 24;
                // friday
if (currentDay === 5) {
    offset = 60;
}              // saturday
else if(currentDay === 6) {                                                                   
    offset = 48;
}

if(currentTime.getHours() > targetHour) {
    console.log('hours:', (targetHour + offset) - currentTime.getHours() - 1);
    console.log('minutes:', 60 - currentTime.getMinutes());
} 
else if(currentTime.getHours() < targetHour) {
    console.log(targetHour - currentTime.getHours() - 1);
    console.log('minutes:', 60 - currentTime.getMinutes());
}