【发布时间】:2014-11-05 21:06:03
【问题描述】:
当我的用户暂停他们的应用程序时,我将日期保存在 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());
}
现在,当应用程序关闭时,将再次触发 ionicplatform.ready 函数以设置最后一个活动日期,因为我无法在关闭时设置一个。我确定其他人必须处理这个问题,并想知道你是如何解决它的。
【问题讨论】:
标签: javascript cordova ionic-framework