21 lines
588 B
JavaScript
21 lines
588 B
JavaScript
![]() |
function updateLights(state) {
|
||
|
document.getElementById("red").classList.toggle("red", state === "red");
|
||
|
document.getElementById("green").classList.toggle("green", state === "green");
|
||
|
}
|
||
|
|
||
|
function fetchStatus() {
|
||
|
fetch("/status")
|
||
|
.then(response => response.json())
|
||
|
.then(data => updateLights(data.state))
|
||
|
.catch(console.error);
|
||
|
}
|
||
|
|
||
|
function toggleLight() {
|
||
|
fetch("/toggle", { method: "POST" })
|
||
|
.then(response => response.json())
|
||
|
.then(data => updateLights(data.state))
|
||
|
.catch(console.error);
|
||
|
}
|
||
|
|
||
|
setInterval(fetchStatus, 1000);
|