hi can any one in the platform help me understand the code used to control LED
function digitalWrite(pin,val){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) {
//document.getElementById("javascript_response").innerHTML = "Javascript Response : "+xmlhttp.responseText;
alert(xmlhttp.responseText);
var obj = JSON.parse(xmlhttp.responseText);
if(obj.success=="1"){
alert(obj.value);
}
}
};
xmlhttp.open("GET", base_url+api_key+"/digitalWrite?pin="+pin+"&state="+val+"&deviceName="+d_name,true);
xmlhttp.send();
I understand above is the function we are calling in our project. I don’t understand why the reaydState and status is checked before the url is opened, I mean this “if” statement “if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1”. Shouldn’t this come after the “open” and “send” statement i,e “xmlhttp.open(“GET”, base_url+api_key+”/digitalWrite?pin="+pin+"&state="+val+"&deviceName="+d_name,true);
xmlhttp.send(); ".
Also i didn’t understand what the following statement are doing!!
document.getElementById("javascript_response").innerHTML = "Javascript Response : "+xmlhttp.responseText;
alert(xmlhttp.responseText);
var obj = JSON.parse(xmlhttp.responseText);
if(obj.success=="1"){
alert(obj.value);