我有一个脚本如下。
var time = document.getElementById("picker-dates").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 + " ";
}
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "00:00:00";
}
}, 1000);}
countdownTimeStart();
所以我想在按一下按钮时运行这个脚本。我试了一下
<button class="start" onclick="countdownTimeStart();">Start</button>
但这不起作用。我怎样才能让它发挥作用。有人可以帮我解决这个问题。
答案 0 :(得分:1)
此代码类似于昨天要求的代码,除了它要求输入小时,分钟和秒。链接在这里: JavaScript countdown timer for hour, minutes and seconds when a start button click
你昨天遗漏了几部分代码,关键是你需要考虑当前日期的部分(变量现在)以及计时器的行程距离(变量距离),现在减去输入的小时分和秒。
也应在点击按钮后进行输入。因此,将您从输入选择器日期字段中读取值的代码移动到按钮单击处理程序中。
为此,这是工作代码
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div>Please enter the time in HH:MM:YY</div>
<input type = "text" id = "picker-dates" />
<p id="demo"></p>
<button id="start" class="start" onclick="countdownTimeStart()">Start</button>
</body>
</html>
function countdownTimeStart() {
var time = document.getElementById("picker-dates").value;
time = time.split(':');
var date = new Date();
var countDownDate = date.setHours(time[0], time[1], time[2]);
console.log(countDownDate);
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
}
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// 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 + " ";
}
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "00:00:00";
}
}, 1000);
}