user1901096
user1901096

Reputation: 247

Daily countdown to specific time

I am trying to make a daily countdown, eg lets say each day up to 7pm. Once 7pm is passed, it should start again at 23:59:59 and count down again to next day 7pm.

I wrote a script but nothing shows up. Why is the countdown not displaying?

DEMO: http://jsfiddle.net/8Ab78/

function ShowTime() {
  var now = new Date();
  var hrs = 18-now.getHours();
  var mins = 60-now.getMinutes();
  var secs = 60-now.getSeconds();
      timeLeft = "" +hrs+' hours '+mins+' minutes '+secs+' seconds';
  $("#countdown").html(timeLeft);
}

var countdown;
function StopTime() {
    clearInterval(countdown);
    setInterval(ShowTime ,1000);
}

Upvotes: 0

Views: 2760

Answers (3)

Fizk
Fizk

Reputation: 1114

As I see it, it's for three reasons;

1) You never call ShowTime

2) Instead of $("#countdown").html(str); you should have $("#countdown").html(timeLeft);

3) Your setInterval should be setInterval(ShowTime ,1000);

Upvotes: 1

Abbas
Abbas

Reputation: 14432

Here you set the html of the div tag:

$("#countdown").html(str);

But what is str? Also, what is timeleft? Maybe you mean to say:

var timeLeft = "" +hrs+' hours '+mins+' minutes '+secs+' seconds';
$("#countdown").html(timeLeft );

Upvotes: 1

Sudharsan S
Sudharsan S

Reputation: 15393

replace

setInterval('ShowTime()',1000);

to this

setInterval(ShowTime ,1000);

Fiddle

Upvotes: 1

Related Questions