2016-02-29 38 views
0

我有一个todo列表,每个待办事项都有一个“完成”按钮。一旦用户点击“完成”按钮,它将被禁用。该按钮应在每天凌晨2点启用。 我可以禁用按钮,但我无法在凌晨2点启用它。这是我的代码。在特定时间(凌晨2点)每天在角js上启用按钮

$scope.doneItem = function(todo) { 
    todo.stopSpam = true; 
    $interval(callAtInterval, 3000); 

    function callAtInterval(){ 
     var date = new Date(); 
     if (date.getHours() === 2 && date.getMinutes() ===0){ 
      todo.stopSpam = false; 
     } 
    } 
}; 
+0

如果您在不同的时区使用应用程序,它将成为一个问题更好地利用momentjs从timeZone获取时间并使用它和标志进行播放:-) – Prasad

回答

0

基于您试图以间隔执行此操作的事实,我将假设您将要离开与您的应用程序的页面。你的例子在技术上可以工作可以工作,但只有当间隔恰好落在凌晨2点(忽略秒和毫秒)。我能想到的最简单准确的方法就是快速计算“now”和2 AM之间的时间间隔。

var date = new Date(); //This gets the current date and time 
var timeInterval = new Date(date.getFullYear(), date.getMonth(), date.getDate()+1,2)-date; 
//This gets the difference between the time and 2 AM the next day. 

//Now you can make an accurate interval call 
$interval(callAtInterval, timeInterval); 

这种方式将其限定为2:00 AM 第二天。但是,如果上午1点你不想跳过那一天的凌晨2点以启用它,那么你可以再做一次快速检查。

var timeInterval; 
if(date.getHours()>2) 
    timeInterval = new Date(date.getFullYear(), date.getMonth(), date.getDate(),2)-date; 
else 
    timeInterval = new Date(date.getFullYear(), date.getMonth(), date.getDate()+1,2)-date; 
相关问题