I am new to JavaScript and HTML. I have a webpage with some buttons that I am using as indicator lights. Not only that, but I set up the function to get a binary byte from the server and to set the lights on or off, only nothing happens. I am not sure how you are even supposed to debug something like this, but perhaps someone can spot and error.
<script>
function UpdateStatusLED() {
var xhttp = new
XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
int Status= Integer.parseInt(this.responseText);
if(Status & 1 == 1) {document.getElementById("OPENP").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("OPENP").style.background = '#f20505'; /* red*/;}
if(Status & 2 == 1) {document.getElementById("CLOSEDP").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("CLOSEDP").style.background = '#f20505'; /* red*/;}
if(Status & 4 == 1) {document.getElementById("OPENM").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("OPENM").style.background = '#f20505'; /* red*/;}
if(Status & 8 == 1) {document.getElementById("CLOSEDM").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("CLOSEDM").style.background = '#f20505'; /* red*/;}
if(Status & 16 == 1) {document.getElementById("RAIN").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("RAIN").style.background = '#f20505'; /* red*/;}
if(Status & 32 == 1) {document.getElementById("SAFE").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("SAFE").style.background = '#f20505'; /* red*/;}
if(Status & 64 == 1) {document.getElementById("CYCLE").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById(CYCLE").style.background = '#f20505'; /* red*/;}
if(Status & 128 == 1) {document.getElementById("OPENCMD").style.background = '#3a0cf2' /* blue */;}
else {document.getElementById("OPENCMD").style.background = '#f20505'; /* red*/;}
}
};
xhttp.open("GET", "/UpdateStatusLED", true);
xhttp.send();
}
setInterval(function() {
UpdateStatusLED();
}, 1713);
</script>
<script>
function ButtonPushed(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);
xhr.send();
}
</script>
<script>
function RoofIsOpen() {
var xhttp = new
XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("RoofIsOpen").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/RoofIsOpen", true);
xhttp.send();
}
setInterval(function() {
RoofIsOpen();
}, 1713);
</script>
I put a print in the server, and it seems that the UpdateStatusLED() does never to get. Below the first script are two of the scripts that actually work.
2
Answers
You are missing a " here:
just before CYCLE
In the context you are giving I can only spot 2 errors
1:
2:
Also this refactor will make your life way easier:
To better understand your problem you can open the Developer Tools on your browser and go to the console and see if there’s any error and maybe edit your post with more information.
Without this, I think I can’t help you as you would like to.
See Mozilla’s Developer Tools Article for further explanation.