gpt4 book ai didi

javascript - 单击按钮不起作用时运行 javascript 脚本

转载 作者:行者123 更新时间:2023-11-30 20:33:39 24 4
gpt4 key购买 nike

我有一个脚本如下。

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 countdown timer for hour, minutes and seconds when a start button click

您遗漏了昨天的代码的几个部分,最重要的是您需要考虑当前日期(变量 now)和计时器行进的距离(变量 distance),现在减去输入的时分秒。

还应在单击按钮后进行输入。因此,将您从输入选择器日期字段中读取值的代码移动到按钮单击处理程序中。

为此,这里是工作代码

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

  1. 脚本

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

关于javascript - 单击按钮不起作用时运行 javascript 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com