java web开发,js自动刷新页面过一段时间后页面会崩溃,,,怎么办,,求助大神!!解决了给冲20话费~~

有这样一个加载百度地图的fuction,,,(其他的无关紧要的就不写了,因为整个网页功能上是正确的,,自己感觉是没有影响,,)

function loadXMLDoc()
{var xmlhttp;
var xx,xy,x,i,id,title;
var txt;
if (window.XMLHttpRequest)
{xmlhttp=new XMLHttpRequest();
}
else
{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

。。。。。。。。。

var opts = {
position : point, // 指定文本标注所在的地理位置
offset : new BMap.Size(30, -30) //设置文本偏移量
}
function openInfo(content,e){
var p = e.target;
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗口对象
map.openInfoWindow(infoWindow,point); //开启信息窗口
}
map.clearOverlays();

for (i=0;i<x.length;i++)
{
xx=x[i].getElementsByTagName("x");
xy=x[i].getElementsByTagName("y");
id=x[i].getElementsByTagName("id");
。。。。。。。。。。。。

}

然后问题来了,这是我刷新的
function refresh(){

loadXMLDoc(); //坐标点有变,位置需要不断从xml中读
setInterval(refresh,10000);
}
refresh();
然后这个html撑不过1分钟就要崩溃,,,,怎么办,在loadXMLDoc中有map.clearOverlays(); ,可是还是崩溃,,,

setInterval:

The real delay between func calls for setInterval is less than in the code!

That’s normal, because the time taken by func’s execution “consumes” a part of the interval.

It is possible that func’s execution turns out to be longer than we expected and takes more than 100ms.

In this case the engine waits for func to complete, then checks the scheduler and if the time is up, runs it again immediately.

In the edge case, if the function always executes longer than delay ms, then the calls will happen without a pause at all.

setTimeout:

The recursive setTimeout guarantees the fixed delay (here 100ms).

That’s because a new call is planned at the end of the previous one.

举例:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-18
呵呵,把setinterval改成setTimeout就行了。setInterval只需要调用一次就会不停的循环,你这样的话,会造成很多的refresh被循环调用。

算了,不给你细说了,你自己看看setInterval和setTimeout的区别,然后再考虑下函数执行的过程就会明白了。网页之所以崩溃,是因为执行了太多的refresh函数。本回答被提问者采纳
相似回答