为什么未定义var检查器

Why is the var checker undefined?

本文关键字:检查 var 未定义 为什么      更新时间:2023-09-26

我不明白为什么checker是未定义的(因此不满足==0或==1的条件)。

它似乎在没有现成功能的JS Fiddle中工作,但在实时系统上不工作。

var checker = 0;
$(window).ready(function () {

function wertelesen() {

    alert(checker);
    if ($("#ersterstatus").html() == '1') {
        if (checker === 0) {
            alert(checker);
            var currentTime = new Date();
            var hours = currentTime.getHours();
            var minutes = currentTime.getMinutes();
            var seconds = currentTime.getSeconds();
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (seconds < 10) {
                seconds = "0" + seconds;
            }
            $("#status1time").html(hours + ":" + minutes + ":" + seconds);
            var checker = 1;
        }
        $("#sd1").html('<img src="img/Status01.png" alt="Status aktiv" class="statusleuchte">');
    }
    if ($("#ersterstatus").html() == '0') {
        if (checker === 1) {
            alert(checker);
            var currentTime = new Date();
            var hours = currentTime.getHours();
            var minutes = currentTime.getMinutes();
            var seconds = currentTime.getSeconds();
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (seconds < 10) {
                seconds = "0" + seconds;
            }
            $("#status1time").html(hours + ":" + minutes + ":" + seconds);
            var checker = 0;
        }
        $("#sd1").html('<img src="img/Status00.png" alt="Status inaktiv" class="statusleuchte">');
    }
    setTimeout(wertelesen, 1000);
}
setTimeout(wertelesen, 1000);
});
</script>

您正在将checker从全局范围重新声明为本地范围:

$("#status1time").html(hours + ":" + minutes + ":" + seconds);
var checker = 1;

只需将其更改为:

$("#status1time").html(hours + ":" + minutes + ":" + seconds);
checker = 1;

正如所指出的,你也要做两次:

$("#status1time").html(hours + ":" + minutes + ":" + seconds);
var checker = 0;