虽然这种代码遍地都是,不过实际用的时候还是不那么得心应手,今天写了一个类似的应用,在此做个记录。
1
<script type="text/javascript">
2
//
3
function GetLiveScore()
4
{
5
var xmldoc;
6
if(window.ActiveXObject)
7
{
8
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
9
}
10
else if(document.implementation && document.implementation.createDocument)
11
{
12
xmldoc=document.implementation.createDocument("livescore","doc",null);
13
}
14
xmldoc.async=true;
15
xmldoc.load("../ScoreOdd/LiveScore.aspx");
16
xmldoc.onreadystatechange=function()
17
{
18
if (xmldoc.readyState == 4)
19
{
20
if(xmldoc==null)
21
{
22
alert("error");
23
}
24
else
25
{
26
//
27
var match=xmldoc.getElementsByTagName('Match');
28
if(match!=null && match.length>0)
29
{
30
for(i=0;i<match.length;i++)
31
{
32
var matchID=match[i].getAttribute("matchID");
33
var score=match[i].getAttribute("score");
34
var halfScore=match[i].getAttribute("halfScore");
35
var matchTime=StringToDate(match[i].getAttribute("matchTime"));
36
if(document.getElementById("Score"+matchID)!=null && document.getElementById("HalfScore"+matchID)!=null)
37
{
38
var now=new Date();
39
//
40
if(now>matchTime)
41
{
42
//
43
document.getElementById("Score"+matchID).innerText=score;
44
//
45
if(now>AddMinutes(matchTime,50))
46
{
47
//
48
document.getElementById("HalfScore"+matchID).innerText=halfScore;
49
}
52
}
53
}
54
}
55
}
56
}
57
}
58
}
59
}
60
//日期转换函数
61
function StringToDate(str)
62
{
63
var newDate=new Date();
64
var year=str.substring(0,4);
65
var month=str.substring(5,7)-1;
66
var day=str.substring(8,10);
67
var hour=str.substring(11,13);
68
var minute=str.substring(14,16);
69
newDate.setFullYear(year);
70
newDate.setMonth(month);
71
newDate.setDate(day);
72
newDate.setHours(hour);
73
newDate.setMinutes(minute);
74
return newDate;
75
}
76
//当前时间加上若干分钟后的时间
77
function AddMinutes(now,minute)
78
{
79
var newDate=new Date((now.getTime()+minute*60*1000));
80
return newDate;
81
}
82
//window.setTimeout("GetLiveScore();",1000);
83
window.setInterval("GetLiveScore();",15000);
84
</script>
首先异步的从一个XML接口中读取数据,遍历DOM树,找到对应的值,并进行相应操作即可。
<script type="text/javascript">2
//3
function GetLiveScore() 4
{5
var xmldoc;6
if(window.ActiveXObject)7
{8
xmldoc=new ActiveXObject("Microsoft.XMLDOM");9
}10
else if(document.implementation && document.implementation.createDocument)11
{12
xmldoc=document.implementation.createDocument("livescore","doc",null);13
}14
xmldoc.async=true;15
xmldoc.load("../ScoreOdd/LiveScore.aspx");16
xmldoc.onreadystatechange=function()17
{18
if (xmldoc.readyState == 4)19
{20
if(xmldoc==null) 21
{22
alert("error");23
}24
else25
{26
//27
var match=xmldoc.getElementsByTagName('Match'); 28
if(match!=null && match.length>0)29
{ 30
for(i=0;i<match.length;i++)31
{32
var matchID=match[i].getAttribute("matchID");33
var score=match[i].getAttribute("score");34
var halfScore=match[i].getAttribute("halfScore");35
var matchTime=StringToDate(match[i].getAttribute("matchTime"));36
if(document.getElementById("Score"+matchID)!=null && document.getElementById("HalfScore"+matchID)!=null)37
{38
var now=new Date(); 39
// 40
if(now>matchTime)41
{ 42
// 43
document.getElementById("Score"+matchID).innerText=score;44
//45
if(now>AddMinutes(matchTime,50))46
{47
//48
document.getElementById("HalfScore"+matchID).innerText=halfScore;49
}52
}53
}54
}55
}56
}57
}58
} 59
}60
//日期转换函数61
function StringToDate(str)62
{63
var newDate=new Date();64
var year=str.substring(0,4);65
var month=str.substring(5,7)-1;66
var day=str.substring(8,10);67
var hour=str.substring(11,13);68
var minute=str.substring(14,16);69
newDate.setFullYear(year); 70
newDate.setMonth(month);71
newDate.setDate(day);72
newDate.setHours(hour);73
newDate.setMinutes(minute);74
return newDate;75
} 76
//当前时间加上若干分钟后的时间77
function AddMinutes(now,minute)78
{79
var newDate=new Date((now.getTime()+minute*60*1000));80
return newDate;81
} 82
//window.setTimeout("GetLiveScore();",1000);83
window.setInterval("GetLiveScore();",15000);84
</script>

浙公网安备 33010602011771号