js定义 window.setTimeout("getmessage()",1000);但是并不执行定时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js定义 window.setTimeout("getmessage()",1000);但是并不执行定时相关的知识,希望对你有一定的参考价值。
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
var xmlDoc;
var browse="";
//加载XML解析控件
var fileRoute="RealData.xml"
if (window.ActiveXObject)
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmlDoc.load(fileRoute);
browse="ie";
else if (document.implementation && document.implementation.createDocument)
xmlDoc=document.implementation.createDocument('', '', null);
xmlDoc.load(fileRoute);
browse="ff";
else
alert( '未做与该浏览器的兼容!');
function getmessage()
var c = 0;
var msg='<table border="1" cellspacing="0" cellpadding="0" width="500">';
msg+='<tr><td width="90"></td><td width="100">测点号</td><td width="100">安装位置</td><td width="110">实时值</td><td width="100">单位</td></tr>'
if(browse=="ff")
var cNodes = xmlDoc.getElementsByTagName("item");
for(j=0;j<cNodes.length;j++)
var bookID=xmlDoc.getElementsByTagName("CDH")[j].childNodes[0].nodeValue;
var sortID=xmlDoc.getElementsByTagName("AZWZ")[j].childNodes[0].nodeValue;
var bookTitle=xmlDoc.getElementsByTagName("DATA")[j].childNodes[0].nodeValue;
var bookAuthor=xmlDoc.getElementsByTagName("DW")[j].childNodes[0].nodeValue;
msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
else if(browse=="ie")
var state = xmlDoc.readyState;
if (state == 4)
var oNodes = xmlDoc.selectNodes("/RealData/item");
for(j=0;j<oNodes.length;j++)
var bookID=oNodes[j].childNodes[0].text;
var sortID=oNodes[j].childNodes[1].text;
var bookTitle=oNodes[j].childNodes[2].text;
var bookAuthor=oNodes[j].childNodes[3].text;
msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
msg+='</table>';
//alert(msg)
document.getElementById("bookList").innerHTML=msg;
document.getElementById('txt').value = c;
c = c+1;
window.setTimeout("getmessage()",1000);
</script>
</head>
<body bgcolor='WHITE' onload="return getmessage()">
<div id="bookList" style="width:100%;">
</div>
<table border="0" cellspacing="0" cellpadding="0" width="500">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="text" id="txt">
</body>
</html>
<script language="javascript">
function getmessage()
var xmlDoc;
var browse="";
//加载XML解析控件
var fileRoute="RealData.xml?no="+Math.random();
if(window.ActiveXObject)
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmlDoc.load(fileRoute);
browse="ie";
else if (document.implementation && document.implementation.createDocument)
xmlDoc=document.implementation.createDocument('', '', null);
xmlDoc.load(fileRoute);
browse="ff";
else
alert( '未做与该浏览器的兼容!');
var c = 0;
var msg='<table border="1" cellspacing="0" cellpadding="0" width="500">';
msg+='<tr><td width="90"></td><td width="100">测点号</td><td width="100">安装位置</td><td width="110">实时值</td><td width="100">单位</td></tr>'
if(browse=="ff")
var cNodes = xmlDoc.getElementsByTagName("item");
for(j=0;j<cNodes.length;j++)
var bookID=xmlDoc.getElementsByTagName("CDH")[j].childNodes[0].nodeValue;
var sortID=xmlDoc.getElementsByTagName("AZWZ")[j].childNodes[0].nodeValue;
var bookTitle=xmlDoc.getElementsByTagName("DATA")[j].childNodes[0].nodeValue;
var bookAuthor=xmlDoc.getElementsByTagName("DW")[j].childNodes[0].nodeValue;
msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
else if(browse=="ie")
var state = xmlDoc.readyState;
if (state == 4)
var oNodes = xmlDoc.selectNodes("/RealData/item");
for(j=0;j<oNodes.length;j++)
var bookID=oNodes[j].childNodes[0].text;
var sortID=oNodes[j].childNodes[1].text;
var bookTitle=oNodes[j].childNodes[2].text;
var bookAuthor=oNodes[j].childNodes[3].text;
msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
msg+='</table>';
document.getElementById("bookList").innerHTML=msg;
document.getElementById('txt').value = c;
c = c+1;
window.setTimeout("getmessage()",1000);
</script> 参考技术A 用setinterval()可以实现定时,你的那个是延时1秒,这个用法一样,用来定时1秒后执行 参考技术B 改为window.setTimeout(function()getmessage();,1000);追问
我改变xml的数据,可是页面显示的还是原来的
追答可以定时执行getmessage();,然后像楼下所说的把RealData.xml改为RealData.xml?no="+Math.random();试试
JS定时器_setTimeout
1、
window.setTimeout(A, B);
window.clearTimeout(?);
2、
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html> 4 5 <style type="text/css"> 6 </style> 7 8 <script type="text/javascript" > 9 10 window.onload = function() 11 { 12 var t1 = window.setTimeout(Hello, 500); 13 var t2 = window.setTimeout("Hello()",1000);//使用字符串执行方法 14 //window.clearTimeout(t1);//去掉定时器 15 //window.clearTimeout(t2);//去掉定时器 16 17 var zz = new Tzz(); 18 window.setTimeout(zz.SayHi, 500); 19 window.setTimeout(Tzz.prototype.SayHi01, 500); // ZC: 没有任何反应 20 window.setTimeout(zz.SayBye, 500); 21 window.setTimeout(Tzz.SayBye01, 500); // ZC: 没有任何反应 22 23 Tzz.prototype.SayHi01.call(window); 24 }; 25 26 function Hello() 27 { 28 console.log("hello"); 29 } 30 31 function Tzz() 32 { 33 this.SayHi = function() 34 { 35 console.log("Hi"); 36 }; 37 this.SayHi01 = function() 38 { 39 console.log("Hi01"); 40 }; 41 } 42 43 Tzz.prototype.SayBye = function() 44 { 45 console.log("Bye"); 46 }; 47 Tzz.prototype.SayBye01 = function() 48 { 49 console.log("Bye01"); 50 }; 51 52 </script> 53 54 <div id="div01"> 55 </div> 56 57 </html>
3、
以上是关于js定义 window.setTimeout("getmessage()",1000);但是并不执行定时的主要内容,如果未能解决你的问题,请参考以下文章
js定时器window.setTimeout和setInterval