我正在尝试实现一个倒数计时器,其中用户输入开始日期(必须是用户当前日期和未来日期之间的任何日期)和结束日期(可以是未来的任何日期,如好)。此外,在某些时间间隔(例如分钟或秒),将显示文本。下面是我到目前为止的代码。它只收集输入,之后什么都不做。请帮忙!
// Set the date we're counting down to
var enddate= document.getElementbyId("end_date").innerHTML;
// Update the count down every 1 second
var x = setInterval(function() {
// Get start date and time
var startdate = document.getElementById("start_date").innerHTML;
// Find the distance between startdate and the end date
var distance = enddate - startdate;
// 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 = days + "d " + 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";
}
if (minutes == 20) {
document.getElementById("demo").innerHTML = "Your Heart rate and blood pressure will drop";}
}, 1000);
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="datetime-local" id="start_date" placeholder="Start Date" />
-
<input type="datetime-local" id="end_date" placeholder="End Date" />
<p id="demo"></p>
</body>
</html>
答案 0 :(得分:0)
您有许多小问题。首先,您的日期输入是字符串。它们需要转换为日期。其次,每次运行间隔时都需要减少距离。第三,您需要创建一个函数,以便 js 在输入之后而不是在输入之前运行。第四,一些小的代码调整。
function myfunction(){
// Set the date we're counting down to
var enddate= document.getElementById("end_date").value;
var startdate = document.getElementById("start_date").value;
if(startdate == '' || enddate == ''){
return
}
enddate = new Date(enddate)
startdate = new Date(startdate)
var distance = enddate - startdate;
// Update the count down every 1 second
var x = setInterval(function() {
// Get start date and time
//var startdate = document.getElementById("start_date").innerHTML;
// Find the distance between startdate and the end date
//var distance = enddate - startdate;
// console.log(distance)
// 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 = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
distance = distance - 1000;
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
if (minutes == 20) {
document.getElementById("demo").innerHTML = "Your Heart rate and blood pressure will drop";}
}, 1000);
}
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="datetime-local" id="start_date" onchange='myfunction()' placeholder="Start Date" />
-
<input type="datetime-local" id="end_date" onchange='myfunction()' placeholder="End Date" />
<p id="demo"></p>
</body>
</html>