我的Ionic应用程序(Android)中包含一个简单的秒表计时器。我基本上只保存开始日期,然后检查每秒(mytimeout = $timeout($scope.onTimeout, 1000);
)自那时起它已经过了多少秒。这很好用,但每当我按下主页按钮然后重新打开应用程序(恢复)时,它会在更新到新时间之前显示旧时间,只需几分之一秒。当您退出应用并重新打开它时,这不会发生,因为它只会计算新时间。
任何可以帮助我的人?任何帮助表示赞赏。
更新:倒计时代码:
$scope.onTimeout = function() {
$scope.clock=Math.trunc(timeleft());
mytimeout = $timeout($scope.onTimeout, 1000);
};
//timer counts down from 60 seconds
function timeleft()
{
var currentdate = new Date();
var currentdateseconds = currentdate.getTime()/1000;
var timeleft = 60 - (currentdateseconds - window.localStorage['seconds']);
return timeleft;
}