为什么var checker未定义?

时间:2013-07-24 07:10:32

标签: javascript

我不明白为什么检查器未定义(因此不满足条件=== 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>

1 个答案:

答案 0 :(得分:2)

您正在从全局范围到本地范围重新声明checker

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

只需将其更改为:

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

正如所指出的那样,你也两次这样做:

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