2

This doesn't work in IE. Forever - 00:00:00 Works in Chrome, Firefox. Why? How can I fix it?

function timer()
{
    var now = new Date();
    var enddate = new Date("07,12,2012,23:00:00");
    var totalRemains = (enddate.getTime()-now.getTime());
    if (totalRemains>1)
    {
        var RemainsSec=(parseInt(totalRemains/1000));
        var RemainsFullDays=(parseInt(RemainsSec/(24*60*60)));
        var secInLastDay=RemainsSec-RemainsFullDays*24*3600;
        var RemainsFullHours=(parseInt(secInLastDay/3600));
        if (RemainsFullHours<10){RemainsFullHours="0"+RemainsFullHours};
        var secInLastHour=secInLastDay-RemainsFullHours*3600;
        var RemainsMinutes=(parseInt(secInLastHour/60));
        if (RemainsMinutes<10){RemainsMinutes="0"+RemainsMinutes};
        var lastSec=secInLastHour-RemainsMinutes*60;
        if (lastSec<10){lastSec="0"+lastSec};
        var mcend = Date.parse("Jan 1, 2009, 00:00:00");
        var mcnow = now.getTime();
        var mc = ((mcend-mcnow)/10).toFixed(0).substr(8);
        document.getElementById('timer').innerHTML = '<p class="timeline">TIME LEFT: '+ RemainsFullHours+":"+RemainsMinutes+":"+lastSec+'</p>';
        setTimeout("timer()",10);
    } 
    else {document.getElementById("timer").innerHTML = '<p class="timeline">TIME LEFT: 00:00:00</p>';}
}

<body onload="timer();">

Can you help me please?

1
  • 1
    Never pass a string to setInterval() or setTimeout(). Doing so is as bad as using eval() and it results in unreadable and possibly insecure code as soon as you use variables since you need to insert them into the string instead of passing the actual variable. The proper solution is setInterval(function() { /* your code *) }, msecs);. The same applies to setTimeout(). If you just want to call a single function without any arguments, you can also pass the function name directly: setInterval(someFunction, msecs); (note that there are no () behind the function name) Commented Jul 12, 2012 at 17:15

1 Answer 1

4

The problem is in this line:

var enddate = new Date("07,12,2012,23:00:00");

This is not a date format. Firefox is heroically making sense out of it, don't expect Internet Explorer to help you out the same, IE is ruthless, he walks alone.

The Date object constructor takes several parameters:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day [, hour, minute, second, millisecond ])

When using the dateString option, your date string must conform to the RFC 2822 specification; it works if you use a supported format:

var enddate = new Date("July 12, 2012 23:00:00");

AND, never, never, never pass a string to setTimeout, pass it a function reference instead:

setTimeout(timer,10);

Try it here: http://jsfiddle.net/bVCMe/

Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.