首页 > 解决方案 > 为什么我的 javascript 循环不起作用并使我的 chrome 浏览器崩溃?

问题描述

更新

我为我的浏览器控制台制作了这个 javascript 脚本并添加了一个循环:

var available = false;
while (available === false){
    setTimeout(function(){
        document.getElementById('CONTENU_WIN').contentWindow.location.reload()
        setTimeout(function(){
            var button = document.getElementById('CONTENU_WIN').contentWindow.document.getElementById('boutonSuivant');
            button.click();
            setTimeout(function(){
                // Get value of span text
                var span_Text = document.getElementById('CONTENU_WIN').contentWindow.document.getElementById("compTableau_tbody").innerText;
                console.log(span_Text)
                if (span_Text == "No available appointment") {
                  console.log("NONNNNN");
                } else {
                    // If "No available appointment." not detected, it means that turnos are available. 
                    // Make a sound
                    var typeWriter = new Audio("http://soundbible.com/mp3/School_Fire_Alarm-Cullen_Card-202875844.mp3");
                    typeWriter.play()
                    // Date & Time instance
                    var ts = new Date();
                    console.log(ts.toString());
                    setTimeout(function(){
                        console.log("WAITING FOR YOUR TO TAKE THE TURNO!!!!")
                        }, 200000);
                    available = true;
                    console.log(available);
                }
            }, 5000);
        }, 10000);
    }, 10000);
}

我的目标是执行第一个“if”语句,直到它不能执行,然后转到 else (var available = true;) 但是这个循环使我的浏览器崩溃。我测试了不同的循环和不同的东西: - do / while - while false/true - for - setInterval - ...但我不明白为什么。也许我仍然对循环不太满意。

有人可以帮我解决这个问题吗?谢谢!

标签: javascriptgoogle-chromeconsole

解决方案


我认为您正在重新分配变量的范围available。可用的 var = true; 在你的函数内部是第二个分配的变量,它只在函数内部可用。


推荐阅读