Shop OBEX P1 Docs P2 Docs Learn Events
calling GetResponse multiple times not working? — Parallax Forums

calling GetResponse multiple times not working?

skynuggetskynugget Posts: 172
edited 2011-09-13 20:52 in Accessories
so i amended Mike G.'s restful led demo html code to show/control pins 23-27.
all works as expected except for one thing.
i added multiple calls to GetResponse for each led, thinking it would turn on all of the led's it does not. it only turns on led23. if i comment out the first GetResponse it turns on led24, and so on.

it seemed like it was doing the first GetResponse then bailing on the script, but then i added an alert box for each step, and i get all 5 alert windows, so i am now lost!
does anyone see a problem with what im trying?

thanks!
<body>


<div id="content">
  <h1><span id="time">Testing...</span></h1>
  
  <h2>PIN 23: <span id="pin23">State</span></h2>
  <p>
  	<a href="#" onClick="return getRequest('led/23/on', 'pin23');">LED On</a> :: 
    <a href="#" onClick="return getRequest('led/23/off', 'pin23');">LED Off</a>
  </p>
  
  <h2>PIN 24: <span id="pin24">State</span></h2>
  <p>
  	<a href="#" onClick="return getRequest('led/24/on', 'pin24');">LED On</a> :: 
    <a href="#" onClick="return getRequest('led/24/off', 'pin24');">LED Off</a>
  </p>
  
  <h2>PIN 25: <span id="pin25">State</span></h2>
  <p>
  	<a href="#" onClick="return getRequest('led/25/on', 'pin25');">LED On</a> :: 
    <a href="#" onClick="return getRequest('led/25/off', 'pin25');">LED Off</a>
  </p>
  
  <h2>PIN 26: <span id="pin26">State</span></h2>
  <p>
  	<a href="#" onClick="return getRequest('led/26/on', 'pin26');">LED On</a> :: 
    <a href="#" onClick="return getRequest('led/26/off', 'pin26');">LED Off</a>
  </p>
  
  <h2>PIN 27: <span id="pin27">State</span></h2>
  <p>
  	<a href="#" onClick="return getRequest('led/27/on', 'pin27');">LED On</a> :: 
    <a href="#" onClick="return getRequest('led/27/off', 'pin27');">LED Off</a>
  </p>
  
</div>
<script language="javascript" type="text/javascript">
    	alert("check pin23 now");
	getRequest('led/23/on', 'pin23');
	alert("check pin24 now");
	getRequest('led/24/on', 'pin24');
	alert("check pin25 now");
	getRequest('led/25/on', 'pin25');
	alert("check pin26 now");
	getRequest('led/26/on', 'pin26');
	alert("check pin27 now");
	getRequest('led/27/on', 'pin27');
</script>
</body>

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-09-13 20:25
    There was a bug in ToInteger() of the StringMethods object. Download the latest object from the Google Repository and see if that fixes the problem (http://forums.parallax.com/showthread.php?134429-Lockup-Using-ToInteger-more-than-once). Otherwise post the SPIN\Javascript code related to the GetResponse() methods. Or do you mean getRequest(resource, elementId)?
  • skynuggetskynugget Posts: 172
    edited 2011-09-13 20:34
    not getResponse (sometimes i swear im not all there), i really meant to say getRequest, its almosts your restfull led HTML demo.:
    //XmlHttpRequest object
    var req = getXmlHttpRequestObject();
    var htmlTarget;
    
    function getRequest(resource, elementId) {
     	// handle the case where a querystring is not detected
    	var char = "&";
     	if(resource.indexOf("?", 0) == -1) {
    		char = "?";
    	}
    		
    	if (req.readyState == 4 || req.readyState == 0) {
    		 req.open("GET", resource + char + 'ms=' + new Date().getTime(), true);
    		 req.onreadystatechange = handleResponse;
    		 htmlTarget = elementId;
    		 req.send(null);
    		 return false;
    	}
    }
    
    function handleResponse() {
    	if (req.readyState == 4) {
    		parseState(req.responseXML);
    	 }
    }
    
    function parseState(xDoc) {
    	var target = document.getElementById(htmlTarget);
    	target.innerHTML = xDoc.getElementsByTagName("led")[0].childNodes[0].nodeValue;
    }
    
    function parseTime(xDoc) {
    	var target = document.getElementById(htmlTarget);
    	target.innerHTML = xDoc.getElementsByTagName("time")[0].childNodes[0].nodeValue;
    }
    

    i been keeping pase with the repository, and have the latest stringmethods already.
  • Mike GMike G Posts: 2,702
    edited 2011-09-13 20:40
    You're jamming too may commands through the same handler. Put a setInterval command in between the getRequest calls; replace the alert()s.

    Better yet, why not send one command like led/all/on and handle the logic in server side SPIN?
  • skynuggetskynugget Posts: 172
    edited 2011-09-13 20:52
    that sounds like a fine idea! if i needed to actually turn all of them on i would definitely do that. im just tooling around trying to figure out lil javascript. i did edit the demo restLED code to show the state of the pin without changing it. i don't know how proper my method is it but it works.
    PRI RestLed(id) | pin, state, temp, headerLen
    '' URL => led\pin\on|off|check
    
      ' Get the pin and state
      pin :=  Request.GetPathNode(id, 1)
      state :=  Request.GetPathNode(id, 2)
      
      ' Convert the ASCII pin number to an integer
      bytemove(@numBuff, pin, strsize(pin))
      temp := str.ToInteger(@numBuff)
      
      if(temp < 23 OR temp > 27)
        temp := 23 
    
      if (strcomp(string("check"), state ))
        temp := ina[temp]
        if temp == 1
          state := string("on")
        else
          state := string("off") 
        
      elseif (strcomp(string("on"), state ))
        PinState(temp, 1)
      else
        PinState(temp, 0)
    
      
      bytefill(@numBuff, 0, strsize(pin))
    
      dynamicContentPtr := @tempBuff
      PushDynamicContent(@xmlencode)
      PushDynamicContent(string("<root application='multisocket'><led id='"))
      PushDynamicContent(pin)
      PushDynamicContent(string("'><![CDATA["))
      PushDynamicContent(state)
      PushDynamicContent(string("]]></led></root>"))
        
      ' Build and send the header
      ' Send the value of led= on or off
      headerLen := Response.BuildHeader(string("xml"), 200, false)
      Socket.txTCP(id, @txdata, headerLen)
      StringSend(id, @tempBuff)
    
      bytefill(@tempBuff, 0, TEMP_BUFFER)
      
      return
    
Sign In or Register to comment.