我的脚本如下。
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>
但这不起作用。我该如何工作。有人可以帮助我解决这个问题。
此代码类似于您昨天要求的代码,除了它要求输入小时,分钟和秒。链接在这里:javaScript倒计时计时器持续一个小时,分钟和几秒钟,当单击开始按钮
您从昨天开始丢失了代码的几个部分,至关重要的是您需要在当前日期(变量 Now (和计时器旅行的距离(变量(距离(,现在减去输入的几小时分钟和秒。
还应在按钮点击后进行输入。因此,将代码从输入选择器日期字段中读取值的代码转移到按钮单击处理程序中。
到此为止,这是工作代码
- html
<!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);
}
在这里工作:https://plnkr.co/edit/65ul4tj2ggwpewbfhbgy?p=preview