我想在单击复选框时创建自动刷新。倒计时计时器运行良好,但是当我单击复选框时仍然没有运气
这是我的代码。需要帮助来检查我的代码
// COUNTDOWN METHOD.
window.setInterval(function() {
counter--;
if (counter >= 0) {
var span;
span = document.getElementById("cnt");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
}
}, 1000);
window.setInterval('refresh()', 10000);
// REFRESH OR RELOAD PAGE.
function refresh() {
window.location.reload();
}
<input type="checkbox"> This page will reload in <span id="cnt" style="color:red;">30</span> Seconds
答案 0 :(得分:0)
添加一个onclick
处理程序:
<input type="checkbox" onclick="refresh()">
答案 1 :(得分:0)
在html中,调用应在复选框选择时执行的函数
<input type="checkbox" id="myCheck" onclick="checkboxClicked()"> This page will reload in <span id="cnt" style="color:red;">30</span> Seconds
在javasccript函数中,启动计时器并在30秒后调用刷新。
function checkboxClicked() {
//function gets called when the checkbox is clicked and the counter starts
let counter = 30
window.setInterval(function() {
counter--;
if (counter >= 0) {
var span;
span = document.getElementById("cnt");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
}
}, 1000);
window.setInterval('refresh()', 30000);
}
// REFRESH OR RELOAD PAGE.
function refresh() {
window.location.reload();
}