setInterval 无法按预期使用 phonegap
setInterval not working as expected with phonegap
我已经设置了一个功能,使用 setInterval()
每 X 分钟检查一次用户的位置,这在大多数情况下都按预期工作。然而,在 phone 处于非活动状态一段时间后,间隔似乎会延长,即如果将其设置为 5 分钟,则可能需要长达一个小时的时间来再次检查 phone 是否已停用一段时间时间。这是为了保持 24/7 全天候运行,因此 phone 处于非活动状态将很常见。
这是一个已知问题还是我应该做些什么来防止这种情况发生?
var onDeviceReady = function(){
var preCheckGPSInterval = setInterval(function(){
var watchID = navigator.geolocation.watchPosition(
function(position){
if(position.coords.accuracy < 100){
navigator.geolocation.clearWatch(watchID);
//code to execute
}
},
function(error){
console.log("error");
},
{enableHighAccuracy: true}
);
}, 300000);
}
document.addEventListener("deviceready", onDeviceReady, false);
Is this a known problem or is there something I should be doing to prevent this?
@马蒂,
这不是问题。这是它应该工作的方式。如果应用程序不在前台,则应用程序的资源将从内存中清除并可能存储在交换区 space.
按照建议,您需要在后台强制您的应用 运行。这篇文章会有所帮助。 7 things you should know about Android Manifest xml file.
我已经设置了一个功能,使用 setInterval()
每 X 分钟检查一次用户的位置,这在大多数情况下都按预期工作。然而,在 phone 处于非活动状态一段时间后,间隔似乎会延长,即如果将其设置为 5 分钟,则可能需要长达一个小时的时间来再次检查 phone 是否已停用一段时间时间。这是为了保持 24/7 全天候运行,因此 phone 处于非活动状态将很常见。
这是一个已知问题还是我应该做些什么来防止这种情况发生?
var onDeviceReady = function(){
var preCheckGPSInterval = setInterval(function(){
var watchID = navigator.geolocation.watchPosition(
function(position){
if(position.coords.accuracy < 100){
navigator.geolocation.clearWatch(watchID);
//code to execute
}
},
function(error){
console.log("error");
},
{enableHighAccuracy: true}
);
}, 300000);
}
document.addEventListener("deviceready", onDeviceReady, false);
Is this a known problem or is there something I should be doing to prevent this?
@马蒂,
这不是问题。这是它应该工作的方式。如果应用程序不在前台,则应用程序的资源将从内存中清除并可能存储在交换区 space.
按照建议,您需要在后台强制您的应用 运行。这篇文章会有所帮助。 7 things you should know about Android Manifest xml file.