我有一个todo列表,每个待办事项都带有一个“完成”按钮。一旦用户点击“完成”按钮,它将被禁用。该按钮应在每天凌晨2点启用。 我可以禁用按钮,但我无法在凌晨2点启用它。这是我的代码。
$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 :(得分:0)
根据您尝试按间隔执行此操作的事实,我将假设您将离开您的应用程序的页面。您在示例技术上中执行的操作可以正常工作,但前提是间隔恰好在凌晨2点(忽略秒和毫秒)。我能想到的最简单准确的方法就是快速计算" now"之间的间隔。和凌晨2点。
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);
这种方式会将其限制在第二天的第二天。但是,如果凌晨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;