You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首个JavaScript脚本UpdateStatusLED无法运行,如何调试排查?

问题排查与修复

核心语法错误(导致函数无法执行)

  • 错误1:误用Java语法定义变量
    原代码里的int Status= Integer.parseInt(this.responseText);是Java写法,JavaScript中没有int关键字,且字符串转整数要用内置的parseInt()方法,修复为:

    let Status = parseInt(this.responseText);
    
  • 错误2:引号缺失导致语法断裂
    第7个else分支里document.getElementById(CYCLE")缺少开头的双引号,直接导致整个函数无法被浏览器解析,修复为:

    document.getElementById("CYCLE").style.background = '#f20505';
    
  • 错误3:位运算逻辑错误
    原代码中Status & 2 == 1这类判断,因==优先级高于&,实际执行的是Status & (2 == 1)(结果永远为0),判断逻辑完全失效。可以简化为直接判断位运算结果是否非0:

    if(Status & 2) { ... }
    

调试步骤

  1. 打开浏览器开发者工具(F12)查看Console面板:所有语法、运行时错误都会在这里显示,这是前端调试的第一步。
  2. 在函数开头添加日志:比如在UpdateStatusLED第一行加console.log("UpdateStatusLED 执行中"),确认函数是否被调用。
  3. 给XMLHttpRequest添加错误监听:捕捉请求失败的原因,示例:
    xhttp.onerror = function() {
        console.log("请求失败,状态码:", this.status);
    };
    

修复后的完整函数示例

function UpdateStatusLED() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            let Status = parseInt(this.responseText);
            
            if(Status & 1) {document.getElementById("OPENP").style.background = '#3a0cf2';}
            else {document.getElementById("OPENP").style.background = '#f20505';}

            if(Status & 2) {document.getElementById("CLOSEDP").style.background = '#3a0cf2';}
            else {document.getElementById("CLOSEDP").style.background = '#f20505';}

            if(Status & 4) {document.getElementById("OPENM").style.background = '#3a0cf2';}
            else {document.getElementById("OPENM").style.background = '#f20505';}

            if(Status & 8) {document.getElementById("CLOSEDM").style.background = '#3a0cf2';}
            else {document.getElementById("CLOSEDM").style.background = '#f20505';}

            if(Status & 16) {document.getElementById("RAIN").style.background = '#3a0cf2';}
            else {document.getElementById("RAIN").style.background = '#f20505';}

            if(Status & 32) {document.getElementById("SAFE").style.background = '#3a0cf2';}
            else {document.getElementById("SAFE").style.background = '#f20505';}

            if(Status & 64) {document.getElementById("CYCLE").style.background = '#3a0cf2';}
            else {document.getElementById("CYCLE").style.background = '#f20505';}

            if(Status & 128) {document.getElementById("OPENCMD").style.background = '#3a0cf2';}
            else {document.getElementById("OPENCMD").style.background = '#f20505';}
        }
    };
    xhttp.onerror = function() {
        console.log("UpdateStatusLED 请求失败,状态码:", this.status);
    };
    xhttp.open("GET", "/UpdateStatusLED", true);
    xhttp.send();
}
setInterval(function() {
    UpdateStatusLED();
}, 1713);

内容的提问来源于stack exchange,提问作者Kurt Lanes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:07:51