为什么倒计时在我设置时区时停止?

时间:2018-06-01 17:51:16

标签: javascript

我使用倒计时来获得两次之间的差异。当我使用特殊时间计时器停止,尽管时间在刷新浏览器时有所不同我发现它已经计数但它没有显示计数秒。

<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>

当我使用var now = new Date().getTime();倒计时时。我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

虽然我还不清楚你想要什么,但我相信以下代码表现得如你所愿。诀窍是你需要每隔一段时间更新now

// 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);