Cordova检查关闭应用程序事件并保存日期/时间
Cordova check close app event and save date/time
当我的用户暂停他们的应用程序时,我会将日期保存在window.localStorage
对象中,这样当应用程序恢复时,我可以向服务器发送请求,并查看是否有新消息给他们。这是有效的,但当他们关闭应用程序并重新启动时,我需要再次设置日期,这样我就无法检查在他们关闭和重新启动之间是否有消息,因为这与暂停和恢复不同。这里有一个代码示例来展示我的意思。
这是应用程序启动的时候:
$ionicPlatform.ready(function() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var days = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var now = year + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;
window.localStorage.setItem('lastActive',now);
});
这些是暂停/恢复功能
document.addEventListener("resume", onResume, false);
document.addEventListener("pause", onPause, false);
function onResume() {
if(window.localStorage.getItem('lastActive')){
var lastActive = window.localStorage.getItem('lastActive');
console.log('last seen on: '+lastActive);
$http({method: 'get', url: '***/api.php/inbox/'+window.localStorage.getItem('auth')+'/'+lastActive}).
success(function(data, status, headers, config) {
if(data.new < 1){
$scope.newcount = data.new
}else{
$scope.newcount = data.new
}
}).error(function(data, status, headers, config) {
$cordovaToast.showShortTop("are you connected to the internet?").then(function(success) {
}, function (error) {
alert("are you connected to the internet?");
});
});
}else{
console.log('lastst online: unkown');
}
}
function onPause() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var days = now.getDate();
var hours = now.getHours() + 1;
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var now = year + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;
window.localStorage.setItem('lastActive',now);
window.localStorage.setItem('lastActivejs',new Date());
}
现在,当应用程序get关闭时,ionicplatform.ready函数get再次启动,以设置最后一个活动日期,因为我无法在关闭时设置一个。我相信其他人必须处理这个问题,并想知道你是如何处理的。
这可能对您来说并不理想,但我通过将当前时间设置为每分钟一次的localStorage
来解决这一问题,然后当我的应用程序打开时,我的ready()
函数所做的第一件事就是检查localStorage
值和Date.now()
之间的差异。这听起来像是一个性能杀手,但它在我的应用程序中运行良好–最好在依赖它之前在您的应用程序进行测试。我这样做:
function ticker() {
var n=Date.now();
if (n%60000<20) {
localStorage.setItem("last_tick",n);
}
requestAnimationFrame(ticker);
}
呼叫ticker()
以启动它。requestAnimationFrame()
允许浏览器进行优化。然而,如果你的应用程序进入后台,WebKit引擎会降低调用ticker()
的频率,并可能完全停止它,所以你可能会发现你需要使用setInterval()
,这可能会带来它自己的性能问题(尽管我仍然认为这会很好)。你的问题是其他人是如何绕过这一点的,这对我来说是有效的。
相关文章:
- jQuery日期[时间]选择器-如何保存时间范围(+修改日期格式)
- 如何避免日期损坏按下保存按钮在免费jqgrid内联编辑
- 通过javascript保存带有当前日期和时间的excel文件
- 一旦使用了jQuery日期选择器,无法使“保存更改”按钮不灰
- 存储日期选择器日期并保存在数据库中
- j查询保存时间和到期日期
- 日期未通过使用 jQuery 日期选择器轨道保存在数据库中
- 以可在 Azure 移动服务 (Javascript) 中查询的方式保存日期时间
- 将 JQuery 日期选取器保存到 Bean 中的多个字段 (d/m/y)
- 在MongoDB中保存和排序日期
- 如何显示用户使用文本区域的日期和时间(单击时与本地存储一起保存)
- 在 JavaScript 中保存“没有”时间的日期
- Cordova检查关闭应用程序事件并保存日期/时间
- 保存倒计时计时器中的日期
- 如何保存JavaScript日期'从浏览器到服务器,再返回的时区
- 使用AngularJS在JSON中保存日期()
- 如何保存日期&使用Javascript在Parse.com中的时间
- 如何在html中保存日期
- 根据文件保存日期从网页下载文件
- 使用JQuery Mobile ui-datepicker保存日期/事件