单击复选框时如何创建自动刷新?

问题描述 投票:0回答:2

我想在复选框单击时创建自动刷新。倒数计时器运行良好,但单击复选框仍然没有运气

这是我的代码。需要帮助来检查我的代码

// 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
javascript page-refresh
2个回答
0
投票

添加一个onclick处理程序:

<input type="checkbox" onclick="refresh()">

0
投票

在html中,调用一个应该在复选框选择上执行的函数

<input type="checkbox" id="myCheck" onclick="checkboxClicked()"> This page will reload in <span id="cnt" style="color:red;">30</span> Seconds

在javascript函数中启动计时器并在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();
}
© www.soinside.com 2019 - 2024. All rights reserved.