functionajax(method, url, date,success){varxhr= null;try{
xhr= newXMLHttpRequest();
}catch(e){
xhr= newActiveXObject(‘Microsoft.XMLHTTP‘);
}if(method== ‘get‘ &&date){
url+= ‘?‘+date;
}
xhr.open(method,url,true);if(method== ‘get‘){
xhr.send();
}else{
xhr.setRequestHeader(‘content-type‘,‘application/x-www-form-urlencoded‘);
xhr.send(date);
}
xhr.onreadystatechange= function(){if(xhr.readyState== 4){if(xhr.status== 200){
success&&success(xhr.responseText);
}else{
alert(‘出错了‘+xhr.status);
}
}
}
}
window.οnlοad= function(){varbtn=document.getElementById(‘btn‘);
btn.οnclick= function(){
ajax(‘get‘,‘getNews.php‘,‘‘,function(date){vardate=JSON.parse(date);varoUl=document.getElementById(‘list‘);varvalue= ‘‘;for(vari=0;i
value+= ‘
‘+date[i].title+‘ ‘+date[i].date+‘‘}oUl.innerHTML=value;
});vartimer= null;
clearInterval(timer);
timer=setInterval(function(){
ajax(‘get‘,‘getNews.php‘,‘‘,function(date){vardate=JSON.parse(date);varoUl=document.getElementById(‘list‘);varvalue= ‘‘;for(vari=0;i
value+= ‘
‘+date[i].title+‘ ‘+date[i].date+‘‘}oUl.innerHTML=value;
});
},3000);
};
};