Help to combine functions for ws.textAll

User avatar
humaxoid
Posts: 2
Joined: Fri Jun 18, 2021 4:51 am

Help to combine functions for ws.textAll

Postby humaxoid » Mon Apr 25, 2022 7:16 am

Hi to all!
This way I transmit the remaining time before the timers are turned on.
  1. void remaining_time() {
  2. if (criterion-1) {
  3.     ws.textAll("buffer1=" + String(buffer1));
  4.   }
  5.  
  6. if (criterion-2) {
  7.     ws.textAll("buffer2=" + String(buffer2));
  8.   }
  9.  
  10. if (criterion-3) {
  11.     ws.textAll("buffer3=" + String(buffer3));  
  12.   }
  13. }
In JavaScript:
  1. function onMessage(event) {
  2. var arrayS = event.data.split("=");
  3. switch (arrayS[0]) {       
  4. case 'buffer1': document.getElementById("time1").innerHTML = arrayS[1]; break      
  5. case 'buffer2': document.getElementById("time2").innerHTML = arrayS[1]; break      
  6. case 'buffer3': document.getElementById("time3").innerHTML = arrayS[1]; break  
  7.  }
  8. }
Everything works well. But I need to add in addition to this, the function of transmitting the state of the buttons.
I see it this way.
  1. void button_status() {
  2. if (criterion_butt-1) {
  3.     ws.textAll(String(!ledState1));
  4.   }
  5.  
  6. if (criterion_butt-2) {
  7.     ws.textAll(String(!ledState2 + 2));
  8.   }
  9.  
  10. if (criterion_butt-3) {
  11.    ws.textAll(String(!ledState3 + 4));
  12.   }
  13. }
JavaScript:
  1. function onMessage(event) {
  2.     switch (event.data) {
  3.         case '0': document.getElementById("state1").innerHTML = "OFF"; break
  4.         case '1': document.getElementById("state1").innerHTML = "ON";break
  5.         case '2': document.getElementById("state2").innerHTML = "OFF"; break
  6.         case '3': document.getElementById("state2").innerHTML = "ON"; break
  7.         case '4': document.getElementById("state3").innerHTML = "OFF"; break
  8.         case '5': document.getElementById("state3").innerHTML = "ON"; break
  9.         }
  10.     }
But I don't understand how to combine these two functions correctly. Ie, the main problem
in JavaScript is to somehow distinguish (separate) them from each other.
Could you help me with this issue? If this was an example with comments for understanding, that would be magical!

Who is online

Users browsing this forum: No registered users and 85 guests