Announcement

Collapse
No announcement yet.

JavaScript and JSON

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    JavaScript and JSON

    I've cobbled together a javascript to control and report back status. The control part works, but I found that the feedback comes back before the command is completed and reports the previous state.

    A quick google search found the 'setTimeout' function which works great, but errors if I don't hard code the variables into the function that the setTimeout function triggers. (Sorry for the crappy explanation)

    The function I am using to control a device calls the feedback function after performing the control function.
    PHP Code:
    function deviceCmd(devRef,cmd)    {

         
    xhttp.open('GET','/JSON?request=controldevicebylabel&ref=' devRef '&label=' cmd ,true);
        
    xhttp.send(null);
        var 
    msg document.getElementById('retrieved');

        
    setTimeout('getData(devRef)',1000); 
        
                                    } 
    setTimeout('getData(devRef)',1000) is called a second after the command is sent in order to allow HomeSeer to record the device status change. If I hardcode setTimeout('getData(558)',1000) everyting works, but trying to use a variable(devRef) causes an error. If it helps, this is the HTML for calling deviceCmd.

    PHP Code:
    <button value="lightOn" onClick="deviceCmd('558','on')">Light On</button

    This is the feedback function.
    PHP Code:
    function getData(devRef){
     
            var 
    xhr1 = new XMLHttpRequest();
            
    xhr1.open('GET''/JSON?request=getstatus&ref=' devReftrue); 
            
    xhr1.send(null);
            
    xhr1.onreadystatechange = function() {
              if (
    xhr1.readyState == 4) {
                  
                var 
    status=JSON.parse(xhr1.responseText).Devices[0].value;
                var 
    loc=JSON.parse(xhr1.responseText).Devices[0].location;
                var 
    name=JSON.parse(xhr1.responseText).Devices[0].name;
                var 
    onoff document.getElementById('status');
                        
                
    onoff.innerHTML 'the status of device : ' loc " " name ' is ' status;
              }
            }
          } 
    If anyone has any suggestions, I'm all ears.

    Thanks.
    Don

    #2
    I findally figured this out; so if anyone ever runs into this problem and this post...

    I replaced this statement

    PHP Code:
    setTimeout('getData(devRef)',1000
    with

    PHP Code:
    setTimeout(function(){getData(devRef)},1000); 
    Don

    Comment


      #3
      Thanks.. What is the use case of this? Are you building a Control web page?
      HomeSeer 2, HomeSeer 3, Allonis myServer, Amazon Alexa Dots, ELK M1G, ISY 994i, HomeKit, BlueIris, and 6 "4k" Cameras using NVR, and integration between all of these systems. Home Automation since 1980.

      Comment


        #4
        I was trying to build a javascript toolkit. Work in progress.
        Don

        Comment

        Working...
        X