<script>
function polling() {
// 执行轮询操作 : 异步
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.status == 200 && xmlHttp.readyState == 4){
result = xmlHttp.responseText;
if (result == "true") {
window.location.href = 'welcome.php';
}
}
}
// 后台访问数据库
randnumber = document.getElementById("randnumber").value;
xmlHttp.open("GET", "polling.php?randnumber=" + randnumber, true);
xmlHttp.send();
}
// 每 1000ms 执行一次 polling()函数
setInterval("polling()", 1000);
</script>
用fiddler看下,你的服务器到底返回了什么。如果是服务器的问题,调试"polling.php
你的randnumber是一个元素里面的值,就是你每一秒都想后台请求一次数据,但是如果第一次是false的话,页面不会刷新,也就是randnumber这个Dom元素里面的值不会变化,那么一般来说,请求后台的参数也没有变化,返回的值也不会变化,即还会是false。