-3

This code is supposed to show a seconds countdown on an HTML page. But it does not work at all. What could be wrong? I kinda copied the code from a website, and just added few details.

function timer() {

    var now = new Date().getFullYear;
    var newdate = new Date("March, 19, 2014");

    var SecCount = (newdate - now) / 1000;
    SecCount = Math.round(SecCount);
    if (SecCount === 1) {document.getElementById("days").innerHTML= "يوم واحد"; }
    else if (SecCount === 2) {document.getElementById("days").innerHTML= "يومان"; }
    else if (SecCount > 2 && SecCount < 10) {document.getElementById("days").innerHTML = (SecCount + " أيام"); }
    else {document.getElementById("seconds").innerHTML = (SecCount + " يوم"); }

    setTimeout(timer,1000);
}
AqeelAT
  • 27
  • 7

3 Answers3

0

Do you call timer() function from somewhere in your code besides setTimeout. You need to call it to start executing and call setTimeout

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0

assuming you want the full date that is now for the variable 'now', you should use Date.now. getFullYear will only return 2014 which will give you unexpected results. this solution is only if you want a count down till March 19 2014.

// var now = new Date().getFullYear
var now = Date.now();

the variable day count isn't in the form of days, it should be this instead

// convert the seconds into days
var DayCount = Math.round(SecCount / (3600 * 24));

did you call the function to start the timeout loop ?

timer();

jsfiddle

Community
  • 1
  • 1
Jay Harris
  • 4,201
  • 17
  • 21
0

I wrote a litte fiddle which does the job.

to get the time left over the % operator is very useful!

philipp
  • 15,947
  • 15
  • 61
  • 106