简体   繁体   中英

Javascript Checkbox select/unselect

Im using java script in JSP the problem is i need auto refresh on selectin checkbox it's working fine but on unselecting it's not stopping the auto refresh activity .plz suggest Thanks in advance

function autorefresh() {
    var isChecked = document.getElementById("is_check").checked;
    var time = 0;
    if (isChecked == true) {
        time = setInterval(function () {
            showExport()
        }, 5000);
    } else if (isChecked == false) {
        clearInterval(time);
    }
}

那是因为您要在autorefresh上下文中定义time变量,因此它不再存储时间间隔的ID,而应在函数外部定义它。

Define time variable as global:

window.time=0;     //global declaration
function autorefresh() {
    var isChecked = document.getElementById("is_check").checked;
    if (isChecked == true) {
        time = setInterval(function () {
            showExport()
        }, 5000);
    } else if (isChecked == false) {
        clearInterval(time);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM