在一天的变化调用函数一次 [英] Call function once on day change

查看:143
本文介绍了在一天的变化调用函数一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我似乎无法找出一个非常简单的问题。我使用AngularJS和火力地堡作为后端。

基本上,我有我想每天一次为了做一个值复位运行,在午夜(设置一些对象属性设​​置为false,等等)的功能。当我手动运行它,准确地按预期工作功能工作正常。

不过,我似乎无法得到它的日变化自动运行。

我想在午夜运行,每天一次是函数 Fed.newDay()。它是通过美联储工厂到我的 DTE 工厂注入 - 这使为期一周的天更新的对象,月等等。

目前,在我的 DTE 工厂我有以下对象:

  VAR日期=新的日期();变种DTE = {             的currentdate:功能(){
                ref.update({日期:date.getDate()});                ref.on('child_changed',函数(快照){
                    的console.log('DTE注册新日);
                    Fed.newDay();
                });                返回date.getDate();
             },
}

这里的目标是运行 Fed.newDay()每次当日期改变。我已经存储如下火力地堡中的日期:
哈里森 - >日期 - >日期:23

我的假设是,每一个日期是在服务器上更新时间,该功能将运行基于火力地堡的 ref.on('child_changed')... 方法。当我 .SET 手动的价值,功能运行正常,但不会在当天自动运行的变化在一夜之间。

我使用也试过的setInterval 的setTimeout 来运行函数每86400000毫秒一次(1天) 。我不认为当应用程序没有运行,虽然这些运行。

必须有我俯瞰的东西在这里。感谢您的时间和帮助。

更新:

我只想澄清。

这是我想要做的:

我有两个复选框标志着需要由一组人,用户完成任务的完成。这两个任务只需要进行一次,由任何和只有一个用户,每天一次。经过这一天结束了,我要明确自己的价值(其设置为false),使同一人或不同的人可以检查两盒无论是。

我没有一个服务器上运行,我只使用火力地堡。火力地堡有我有机会获得一个值,即目前正在更新只要有人使用该应用程序的那一天该月的一天。

解决方案:

由于@bmceldowney的帮助。

这是我到达的解决方案。另外,请参见下面的正确答案,以避免如果多个用户在11:59登录时出现的问题。

 变种DTE = {        的currentdate:功能(){
            dateRef.update({日期:date.getDate()});
            返回date.getDate();
        },        setLastUpdated:功能(天){
            dateRef.set({LASTUPDATED:天});
        },        compareReset:功能(最后一个){
            无功电流= Dte.currentDate();
            如果(即止!==电流){
                Fed.newDay();
                Dte.setLastUpdated(电流);
            }其他{
                的console.log('没有复位需要');
            }
        },        载:函数(){
            dateRef.on('值',函数(快照){
                如果(snapshot.val()!= NULL){
                    VAR数据= snapshot.val();
                    VAR updateValue =数据['LASTUPDATED'];
                    Dte.compareReset(updateValue);
                }其他{
                    Dte.load();
                }
            })
        },


解决方案

您可以在 LASTUPDATED 场火力存储。在客户端负载,抢 LASTUPDATED 的价值,并将其与当前的日期。如果是不同的,运行 Fed.newDay 方法和更新 LASTUPDATED 日期。

编辑:

由于TheSharpieOne在评论中指出的那样,你需要某种形式的运行,以确保您得到适当的行为,如果一个客户在午夜钟声敲响的已经登录定时器功能。这使你持开放的态度,可能会导致 Fed.newDay 被多次调用,如果多个用户在该时间记录到一个潜在的竞争条件。这些类型的问题几乎是不可能修复不具有适当的管理状态的正确的服务器。

I have a really straightforward problem that I can't seem to figure out. I'm using AngularJS and Firebase as a backend.

Basically, I have a function that I want to run once, every day, at midnight in order to do a value reset (set some object properties to false, etc.). The function works fine when I run it manually and works exactly as intended.

However, I can't seem to get it to run automatically on day change.

The function I want to run at midnight, once each day is Fed.newDay(). It's injected through the Fed factory to my Dte factory - which keeps an updated object with the days of the week, month etc.

Currently, in my Dte factory I have the following object:

var date = new Date();    

var Dte = { 

             currentDate: function() {
                ref.update({date: date.getDate()});

                ref.on('child_changed', function(snapshot) {
                    console.log('Dte registered new Day');
                    Fed.newDay();
                });

                return date.getDate();
             },
}

The goal here is to run Fed.newDay() once each time the date changes. I have stored the date in Firebase as follows: harrison -> date -> date: 23

My assumption was that each time the date was updated on the server, the function would run based on Firebase's ref.on('child_changed')... method. When I .set the value manually, the function runs as expected, but will not run overnight on the automatic day change.

I've also tried using setInterval and setTimeout to run the function once every 86400000 milliseconds (1 day). I don't think these run when the application isn't being run, though.

There has to be something I'm overlooking here. Thanks for your time and help.

Update:

Just to clarify.

This is what I'm trying to do:

I have a two checkboxes that mark completion of a task that needs to be done by a group of people, the users. These two tasks only need to be done once, by any and only one user, once per day. After that day is over, I want to clear their values (set them to false) so that the same person OR a different person can check either of the two boxes.

I don't have a server running, I'm only using Firebase. Firebase has a value that I do have access to, that is currently updating the day of the month as long as someone uses the application that day.

Solution:

Thanks to @bmceldowney for the help.

This is the solution I arrived at. Also, please see the correct answer below in order to avoid problems that may arise if multiple users log in at 11:59.

    var Dte = {

        currentDate: function() {
            dateRef.update({date: date.getDate()});
            return date.getDate();
        },

        setLastUpdated: function(day) {
            dateRef.set({lastUpdated: day});
        },

        compareReset: function(last) {
            var current = Dte.currentDate();
            if (last !== current) {
                Fed.newDay();
                Dte.setLastUpdated(current);
            } else {
                console.log('no reset needed');
            }
        },

        load: function() {
            dateRef.on('value', function(snapshot) {
                if (snapshot.val() != null) {
                    var data = snapshot.val();
                    var updateValue = data['lastUpdated'];
                    Dte.compareReset(updateValue);
                } else {
                    Dte.load();
                }
            })
        },

解决方案

You can store a lastUpdated field in firebase. On client load, grab the value of lastUpdated and compare it to the current date. If it's different, run your Fed.newDay method and update the lastUpdated date.

EDIT:

As the TheSharpieOne points out in the comments, you'll want some sort of timer function running to make sure you get the appropriate behavior if a client is already logged in at the stroke of midnight. This leaves you open to a potential race condition that could result in Fed.newDay being called multiple times if multiple users are logged in at that time. These kinds of issues are almost impossible to fix without having a proper server that manages state appropriately.

这篇关于在一天的变化调用函数一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆