Javascript倒计时器无法正常工作

时间:2018-04-27 16:14:34

标签: javascript html countdowntimer

我正在创建一个javascript函数来倒计时用户选择的值。到目前为止,这是我的代码。



function countdownTimeStart() {
  var time = document.getElementById("test").value;
  time = time.split(':');
  var date = new Date();
  var countDownDate = date.setHours(time[0], time[1], time[2]);
  var x = setInterval(function () {
    // Get to days date and time
    var now = new Date().getTime();

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

    // Time calculations for days, hours, minutes and seconds
    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("demo1").innerHTML = hours + ": "
      + minutes + ": " + seconds + " ";

    // If the count down is over, write some text
    if (distance < 0) {
      clearInterval(x);
      document.getElementById("demo1").innerHTML = "00:00:00";
    }
  }, 200);
}
document.querySelector("button").addEventListener(
  "click",
  countdownTimeStart
)
&#13;
<input type = "test" id = "test" value="20:12:40">
<div id="demo1"></div>
<button>start</button>
&#13;
&#13;
&#13;

问题是倒计时不能正常工作。倒计时从另一个时间开始,而不是从输入标签的20:12:40开始。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

只需将countDownDate设置为当前时间加上您需要的未来小时数,分钟数和秒数。

var countDownDate = new Date( date.getTime() 
                            + parseInt(time[0])*(1000 * 60 * 60) //hours
                            + parseInt(time[1])*(1000 * 60) //minutes
                            + parseInt(time[2])*1000 ); //seconds

答案 1 :(得分:1)

如果您想倒计时,以下情况应该有效:

&#13;
&#13;
var time = document.getElementById("test").value;
time = time.split(':');
var date = new Date();
var countDownDate = date.setHours(time[0], time[1], time[2]);

function countdownTimeStart() {
  var x = setInterval(function () {

    // set hours, minutes and seconds, decrease seconds
    var hours = time[0];
    var minutes = time[1];
    var seconds = time[2]--;
    
    // if seconds are negative, set them to 59 and reduce minutes
    if (time[2] == -1) {
    	time[1]--;
        time[2] = 59
    }
    
    // if minutes are negative, set them to 59 and reduce hours
    if (time[1] == -1) {
    	time[0]--;
        time[1] = 59
    }

    // Output the result in an element with id="demo"
    // add leading zero for seconds if seconds lower than 10
    if (seconds < 10) {
        document.getElementById("demo").innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
    } else {
        document.getElementById("demo").innerHTML = hours + ": " + minutes + ": " + seconds + " ";
    }
  }, 1000);
}

document.querySelector("button").addEventListener(
  "click",
  countdownTimeStart
)
&#13;
<input type = "text" id = "test" value="20:00:01">
<div id="demo"></div>
<button>start</button>
&#13;
&#13;
&#13;