0

Hi guys I have a countdown timer that is meant to run on a website. Unfortunately it isn't supported in Firefox, though it runs fine in Chrome and Opera (other browsers yet to be checked).

The theory behind it is that a time stamp, "END", is set on a page and the saved in a DB (all working fine). END is then retrieved (PHP) and fed into a jQuery function which counts down from the current time to END.

the php is all working as it should so my jQuery function is shown below as I'm sure it is located in here somewhere.

Thanks in advance guys!

// Code for a countdown timer
var countdown = function(end, elements){
    var _second = 1000,
        _minute = _second*60,
        _hour = _minute*60,
        _day = _hour*24,

        finish = new Date(end),
        timer,

        calculate = function(){

            var now = new Date(),
                remaining = finish.getTime() - now.getTime(),
                data;

            if (isNaN(finish)){
                console.log("Invalid date/time");
                return;
            };

            if (remaining <= 0){
                clearInterval(timer);
                if (typeof callback ==='function'){
                    callback();
                }
            } else {
                if (!timer){
                    timer = setInterval(calculate, _second);
                };

                data = {
                    "days" : Math.floor(remaining/_day),
                    "hours" : Math.floor((remaining % _day) / _hour),
                    "minutes" : Math.floor((remaining % _hour) / _minute),
                    "seconds" : Math.floor((remaining % _minute) / _second)
                };

                if (elements.length){
                    for (x in elements){
                        var x = elements[x];
                        data[x] = ('00' + data[x]).slice(-2);
                        document.getElementById(x).innerHTML = data[x];
                    };
                };
            };
        };
    calculate();
}

sorry, it is then called as follows:

<script>
    var callback = function(){console.log('Done!')};
    countdown("<?php get_deadline($conn);?>", ['days', 'hours', 'minutes', 'seconds'], callback); // Get deadline for exam name
</script>

As I say the php function is working fine!

Eoin
  • 357
  • 1
  • 4
  • 20
  • Could you try to give something simpler, rather than the snippet of code that's problematic? That is, an [MCVE](http://stackoverflow.com/help/mcve). – Nic Feb 23 '15 at 15:53
  • 1
    any errors thrown? What format is date string? My guess is you are providing invalid date format – charlietfl Feb 23 '15 at 16:14
  • just checked, you're dead right....found it strange because all my other jQuery code ran flawlessly on the site! Thank you very much!!! – Eoin Feb 23 '15 at 16:17

1 Answers1

0

I found the solution on another tread, located here

All it took was to make the format readable across browsers as @charlietfl proposed.

the code I used was on line 8 of the original script:

finish = new Date(end.replace(/\-/g,'\/').replace(/[T|Z]/g,' ')),
         timer,
Community
  • 1
  • 1
Eoin
  • 357
  • 1
  • 4
  • 20