-4

I need to create a countdown that runs daily from 8am to 8pm Monday-Friday and 8am-6pm Saturday and Sunday and then when it's finished, says something like 'We open tomorrow at 8am!'

How can I achieve this?

Website: (you can see where the countdown would be.)

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>

Here's a few i've tried.

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);
  };
 }
14
  • 4
    "anything else needed" ... yes, some kind of attempt. Commented Jan 31, 2014 at 20:08
  • What have you tried so far? There are plenty of countdown plugins out there if you want a pre-made solution. Commented Jan 31, 2014 at 20:08
  • If you have so litte knowledge in JS that you can't even attempt anything, you probably shouldn't turn to this site for help. Commented Jan 31, 2014 at 20:09
  • I have tried a few but I can't find any that are set to daily times like 8am-8pm M-F and 8am-6pm S-S. Most countdowns seem to be to a specific date and do not restart daily. Commented Jan 31, 2014 at 20:09
  • You're going to need to increase your knowledge of java script if you expect to do something like this. Even if someone spends the time to write a method for you, how will you support it if you don't understand it? Commented Jan 31, 2014 at 20:10

1 Answer 1

2

Since everyone is ripping you apart and being of no help, here's a code that is similar to what you may want. Adjust accordingly.

// 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());
} 
Sign up to request clarification or add additional context in comments.

1 Comment

No one is ripping anyone, these are the rules of Stack Overflow - blatant code requests are no benefit to anyone but self-serving posters who have no interest in learning and only serve to perpetuate a cycle that is polluting the site and diluting it's quality as a learning resource.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.