javascript time counter problem with safari browser

Viewed 439

i have a time counter in my web pages with below JavaScript code:

function setTimeCounters() {
    $(".time-counter").each(function (index, elem) {

        let exp = new Date($(this).attr('data-time'));

        var x = setInterval(function() {

            let now = (new Date()).getTime();
            let ex = (new Date(exp)).getTime();

            let distance = ex - now;


            let days = Math.floor(distance / (1000 * 60 * 60 * 24));
            let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            let seconds = Math.floor((distance % (1000 * 60)) / 1000);

            $(elem).find(".time").html(`${showNumber(days)}:${showNumber(hours)}:${showNumber(minutes)}:${showNumber(seconds)}`);

            if ((hours+minutes+seconds) <= 0) {
                clearInterval(x);
                $(elem).html("timeout!");
            }
        }, 1000);

    });
}

function showNumber (num) {return ("0" + num).slice(-2);}

setTimeCounters();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="time-counter"
  data-time="2020-09-25 05:20:06">
  <span class="time"></span>
  <span><i class="fa fa-clock"></i></span>
</div>

my problem is with safari browser on iphone ios and maybe mac os(work fine in chrome , ...) show time like this : aN:aN:aN:aN:aN

1 Answers

check browser support for let. I strongly reccomend to use var instead of let reference url: https://caniuse.com/let

Related