谢谢大家了,不过感觉还又个疑惑,就是当我们在网页上点击"Toggle LED"时,应该是执行网页文件中的toggle_led()函数吧?代码如下:
function ledComplete()
{
if(led.readyState == 4)
{
if(led.status == 200)
{
document.getElementById("ledstate").innerHTML = " " + led.responseText + " ";
}
}
}
if(window.XMLHttpRequest)
{
req = new XMLHttpRequest();
led = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
led = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req)
{
req.open("GET", "/cgi-bin/toggle_led?id" + Math.random(), true);
req.send(null);
}
if(led)
{
led.open("GET", "/ledstate?id=" + Math.random(), true);
led.onreadystatechange = ledComplete;
led.send(null);
}
从上面代码顺序看,函数一开始就执行:document.getElementById("ledstate").innerHTML = " " + led.responseText + " ";
把改变了的状态写入指定位置,可是这个时候下面服务器应该还没有返回数据吧?请问大虾们这个问题应该怎么理解呢? |