ajax定时调用的方法介绍
来源:爱站网时间:2022-08-05编辑:网友分享
这篇文章主要讲解了ajax定时调用的方法介绍,对这方面比较感兴趣的小伙伴不妨看看爱站技术频道小编所整理的资料,相信此内容能在工作中帮助到你解决问题所在。
代码如下:
function initXMLRequest(){
if (window.ActiveXObject) {
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
xmlRequest = new XMLHttpRequest();
}
}
}
function sendHTTPRequest(){
initXMLRequest();
var url = "monitor!taskdata.action"; //调用的servlet
if (xmlRequest) {
xmlRequest.open("POST", url, true);
xmlRequest.send(null);
xmlRequest.onreadystatechange = isDataExists;
//使用方法回调,每5秒调用一次
setTimeout("sendHTTPRequest()",5000);
}
}
function isDataExists(){
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
var data = eval("("+xmlRequest.responseText+")");
// 获取tbody的表格内容
var taskTable = document.getElementById("tasktbody");
for(var i=taskTable.childNodes.length-1;i>=0;i--){
taskTable.removeChild(taskTable.childNodes[i]);
}
$.each(data, function(i,val){
var newTR = taskTable.insertRow(taskTable.rows.length);
newTR.id="taskdata_"+(i+1);
newTR.ondblclick=getdetail;
newTR.onclick=selectRow;
backColor(val.taskstatus,newTR);
var newNameTD=newTR.insertCell(0);
newNameTD.innerHTML = val.taskname;
newNameTD.id="taskname_"+(i+1);
var newStatusTD=newTR.insertCell(1);
newStatusTD.innerHTML = val.taskstatus;
newStatusTD.id="taskstatus_"+(i+1);
var newArgTD=newTR.insertCell(2);
newArgTD.innerHTML = val.data_start_time;
var newOtherArgTD=newTR.insertCell(3);
newOtherArgTD.innerHTML = val.schedule_interval;
var newdateformatTD=newTR.insertCell(4);
newdateformatTD.innerHTML = val.next_execution_time;
var newbackwordTD=newTR.insertCell(5);
newbackwordTD.innerHTML = val.taskdetailscount;
var newpriorityTD=newTR.insertCell(6);
newpriorityTD.innerHTML = val.statusruncount;
var newleftTD=newTR.insertCell(7);
newleftTD.innerHTML = val.statusfinishcount;
var newlefttimeTD=newTR.insertCell(8);
newlefttimeTD.innerHTML = val.statuserrorcount;
});
}
}
}
if (window.ActiveXObject) {
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
xmlRequest = new XMLHttpRequest();
}
}
}
function sendHTTPRequest(){
initXMLRequest();
var url = "monitor!taskdata.action"; //调用的servlet
if (xmlRequest) {
xmlRequest.open("POST", url, true);
xmlRequest.send(null);
xmlRequest.onreadystatechange = isDataExists;
//使用方法回调,每5秒调用一次
setTimeout("sendHTTPRequest()",5000);
}
}
function isDataExists(){
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
var data = eval("("+xmlRequest.responseText+")");
// 获取tbody的表格内容
var taskTable = document.getElementById("tasktbody");
for(var i=taskTable.childNodes.length-1;i>=0;i--){
taskTable.removeChild(taskTable.childNodes[i]);
}
$.each(data, function(i,val){
var newTR = taskTable.insertRow(taskTable.rows.length);
newTR.id="taskdata_"+(i+1);
newTR.ondblclick=getdetail;
newTR.onclick=selectRow;
backColor(val.taskstatus,newTR);
var newNameTD=newTR.insertCell(0);
newNameTD.innerHTML = val.taskname;
newNameTD.id="taskname_"+(i+1);
var newStatusTD=newTR.insertCell(1);
newStatusTD.innerHTML = val.taskstatus;
newStatusTD.id="taskstatus_"+(i+1);
var newArgTD=newTR.insertCell(2);
newArgTD.innerHTML = val.data_start_time;
var newOtherArgTD=newTR.insertCell(3);
newOtherArgTD.innerHTML = val.schedule_interval;
var newdateformatTD=newTR.insertCell(4);
newdateformatTD.innerHTML = val.next_execution_time;
var newbackwordTD=newTR.insertCell(5);
newbackwordTD.innerHTML = val.taskdetailscount;
var newpriorityTD=newTR.insertCell(6);
newpriorityTD.innerHTML = val.statusruncount;
var newleftTD=newTR.insertCell(7);
newleftTD.innerHTML = val.statusfinishcount;
var newlefttimeTD=newTR.insertCell(8);
newlefttimeTD.innerHTML = val.statuserrorcount;
});
}
}
}
ajax定时调用的方法介绍不知道各位小伙伴都看明白了没有,在我们爱站技术频道网站有很多精彩的技术文章内容,如果觉得还不错,可以随时来关注收藏下以备不时之需。
下一篇:Ajax设计修改购物车的思路