使用 AJAX 进行异步加载轮询操作。简单代码如下:

<script>

// 执行ajax轮循操作
function polling(){
var xmlhttp;
// 判断浏览器--创建对象
if ( window.XMLHttpRequest ){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
// 与服务器建立连接
xmlhttp.open( "GET", "polling.php", true );
// 发送请求
xmlhttp.send();
// 监听事件 回调匿名函数 判断返回值
xmlhttp.onreadystatechange = function(){
if ( xmlhttp.status == 200 && xmlhttp.readyState ==4 ){
alert( xmlhttp.responseText );
}
}
}
setInterval( "polling()", 1000 );

</script>


posted on 2016-10-10 10:25  啥也不会的程序猿  阅读(274)  评论(0编辑  收藏  举报