2014-11-03 64 views
0

我想在用户的系统日期更改时刷新我的页面内容。 用户在03 - 11 - 2012年11月3日中午访问该页面,当系统日期更改为04-11-2014(11月4日)时,应刷新内容或调用函数。AngularJS:系统日期更改时调用函数

我们可以使用$interval(checksysDate(),5000)其中checksysDate()将检查日期是否以5000的间隔更改。但是,我想知道我们是否可以在系统日期使用$scope.$watch(仅限日期,即11月3日或4日,而不是时间)。
任何优雅的解决方案?

感谢,

+0

http://stackoverflow.com/a/14236033/ 1182982可能是有用的 – Yasser 2014-11-03 07:17:08

+0

Yasser的评论。如果你了解摘要循环,你就会明白你的想法为什么不起作用。 但是,为什么你只是没有实现一个超时你将剩余时间设置为24 - 当前时间?还是比间隔更好... – Charminbear 2014-11-03 07:18:32

+0

同意@Charminbear。最好不要使用$ watch。计算打开页面时的时间间隔。和setTimeout(第二天 - 现在)更改日期(然后使用间隔触发日期更改每24小时)。 – 2014-11-03 07:31:00

回答

0

你可以做这样的事情,但它是不漂亮...

angular.module("test", []).controller("TimeCtrl", function($scope, $interval) { 
 
    $scope.seconds = new Date().getSeconds(); 
 
    $scope.nowSeconds = $scope.seconds; //this one is just for presentation 
 

 
    $interval(function() { 
 
    this.seconds = new Date().getSeconds(); //you can put here day 
 
    }.bind($scope), 5000); 
 

 
    $scope.$watch('seconds', function(oldV, newV) { 
 
    if(oldV !== newV) 
 
     $scope.nowSeconds = newV; //you can put your reaction here - reload page 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="test" ng-controller="TimeCtrl"> 
 
    {{nowSeconds}} 
 
</div>