MR.alaa
MR.alaa

Reputation: 92

Why does the countdown stop when I set the time zone?

I have used a countdown to get the difference between two times. When I use the special time the timer stops despite time gets the difference when refreshing the browser I find it already counts but it doesn't show counting seconds.

<p id="demo"></p>

<script>

    // given the city's UTC offset
function calcTime(city, offset) {

// create Date object for current location
d = new Date();

// convert to msec
// add local time zone offset 
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);

// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));

// return time as a string
return nd;

}

  var xx = calcTime('country1', '+3');
  var x1 = calcTime('country2', '-1');
// Set the date we're counting down to
var countDownDate = new Date(xx).getTime();
var now = new Date(x1).getTime();

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time      //1527885631789
    //var now2 = new Date().getTime();  //1527871237519

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML =  hours + "h "
    + minutes + "m " + seconds + "s ";

    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
</script>

When I use var now = new Date().getTime(); the countdown works. What is the problem in my code ?

Upvotes: 0

Views: 50

Answers (1)

lucasvw
lucasvw

Reputation: 1557

Although I'm still not clear on what you want, I believe the following code behaves how you want. The trick is that you need to update now at every interval.

// given the city's UTC offset
function calcTime(city, offset) { .. unchanged ..}

var xx = calcTime('country1', '+3');
// Set the date we're counting down to
var countDownDate = new Date(xx).getTime();

// Update the count down every 1 second
var x = setInterval(function() {
    // calculate the current time
    var now = new Date(calcTime('country2', '-1')).getTime();

    // Get todays date and time      //1527885631789
    //var now2 = new Date().getTime();  //1527871237519

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    console.log(hours + "h " + minutes + "m " + seconds + "s ");

    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        console.log("EXPIRED");
    }
}, 1000);

Upvotes: 1

Related Questions