Javascript连续重定向
我有这个页面加载罚款一次 基本上,它是拉一个gps位置,然后我希望它做重定向每10秒拉gps数据再次 但它不会每10秒就持续重定向一次 怎么了-从表面上看,它应该Javascript连续重定向,javascript,gps,Javascript,Gps,我有这个页面加载罚款一次 基本上,它是拉一个gps位置,然后我希望它做重定向每10秒拉gps数据再次 但它不会每10秒就持续重定向一次 怎么了-从表面上看,它应该 <script type="text/javascript"> // Get a single location update function getLocationConstant() { if(navigator.geolocation) { navigator.geoloca
<script type="text/javascript">
// Get a single location update
function getLocationConstant()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError,{enableHighAccuracy:true,maximumAge:0});
} else {
alert("Your browser or device doesn't support Geolocation");
}
}
// If we have a successful location update
function onGeoSuccess(event)
{
document.getElementById("Latitude").value = event.coords.latitude;
document.getElementById("Longitude").value = event.coords.longitude;
document.getElementById("location").href = "track2.cfm?track=s&GPSLat=" + event.coords.latitude + "&GPSLong=" + event.coords.longitude;
redirectUrl = "track2.cfm?track=y&GPSLat=" + event.coords.latitude + "&GPSLong=" + event.coords.longitude;
gpslat = event.coords.latitude;
gpslong = event.coords.longitude;
}
// If something has gone wrong with the geolocation request
function onGeoError(event)
{
alert("Error code " + event.code + ". " + event.message);
}
function redirect()
{
window.location = redirectUrl;
}
setTimeout(redirect,10000);
</script>
//获取单个位置更新
函数getLocationConstant()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError,{EnableHighAccurance:true,maximumAge:0});
}否则{
警报(“您的浏览器或设备不支持地理定位”);
}
}
//如果我们有一个成功的位置更新
功能onGeoSuccess(事件)
{
document.getElementById(“纬度”).value=event.coords.Latitude;
document.getElementById(“经度”).value=event.coords.Longitude;
document.getElementById(“位置”).href=“track2.cfm?track=s&GPSLat=“+event.coords.latitude+”&GPSLong=“+event.coords.longitude;
redirectUrl=“track2.cfm?track=y&GPSLat=“+event.coords.latitude+”&GPSLong=“+event.coords.longitude;
gpslat=event.coords.latitude;
gpslong=event.coords.longitude;
}
//如果地理定位请求出现问题
函数onGeoError(事件)
{
警报(“错误代码”+事件代码+”+事件消息);
}
函数重定向()
{
window.location=重定向URL;
}
设置超时(重定向,10000);
这里有几个问题
1) 正如jay c所说,回调只运行一次。要解决这个问题,您可以按照setInterval所说的做,或者在重定向结束时,您只需再次设置计时器
function redirect() {
window.location = redirectUrl;
setTimeout(redirect, 10000);
}
2) 如果位置没有改变,URL总是相同的。浏览器可能会注意到这一点,而不会刷新url。您可以通过一些窗口重新加载操作来强制它,也可以通过在url上附加时间戳来保证url的新鲜度
var currentTime = new Date();
var milliseconds = currentTime.getTime();
redirectUrl = "track2.cfm?track=y&GPSLat=" + event.coords.latitude + "&GPSLong=" + event.coords.longitude + "&time=" + milliseconds;
这可能是一个范围问题。看起来重定向URL可能是其设置值的原始函数的本地函数。我可能错了。这和你之前问的问题不一样吗?然后,与现在一样,不清楚如何调用
onGeoSuccess
函数,或者是否调用了该函数-至关重要,因为这是设置重定向字符串的地方。我正在尝试将问题分开,而不是插入post-重定向在以前的版本中确实有效-但不会使用新的gps数据持续重定向。您的代码没有显示如何调用getLocationConstant()。也许最好是每间隔调用getLocationConstant(),而不是重新加载页面。