以下是我的JavaScript代码片段。它没有按预期运行,请帮助我。XMLHttpRequest泄漏
<script type="text/javascript">
function getCurrentLocation() {
console.log("inside location");
navigator.geolocation.getCurrentPosition(function(position) {
insert_coord(new google.maps.LatLng(position.coords.latitude,position.coords.longitude));
});
}
function insert_coord(loc) {
var request = new XMLHttpRequest();
request.open("POST","start.php",true);
request.onreadystatechange = function() {
callback(request);
};
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send("lat=" + encodeURIComponent(loc.lat()) + "&lng=" + encodeURIComponent(loc.lng()));
return request;
}
function callback(req) {
console.log("inside callback");
if(req.readyState == 4)
if(req.status == 200) {
document.getElementById("scratch").innerHTML = "callback success";
//window.setTimeout("getCurrentLocation()",5000);
setTimeout(getCurrentLocation,5000);
}
}
getCurrentLocation(); //called on body load
</script>
我想要实现的是每隔5秒左右发送一次我的当前位置到php页面。我可以在我的数据库中看到很少的坐标,但过了一段时间后就变得很奇怪了。 Firebug显示非常奇怪的日志,例如不定期的同时POST。
这里的萤火虫截图:
有程序中的漏洞。请帮忙。
编辑:在Firebug控制台预期的结果应该是这样的: -
位置
门柱内侧....
内回调/* 5秒后*/
内部位置
POST ...
inside callba CK/*不断重复*/
看看这里:http://jsfiddle.net/ayLs7/它没有调用你的特定的AJAX页面,它再现你的问题。我没有看到它产生了你所描述的问题,即使让它运行了一段时间。我怀疑这个问题不在XHR中,但可能在你的Javascript的其他领域。 – Ryley 2010-06-04 18:05:53