Search code examples
meteorresetclockvartimed

Meteor Timed variable resets


I'm making a game with meteor and I want certain variables to reset at the end of everyday.. DO you know how I would go about doing that? Like at the end of everyday the amount of turns goes back to ten.


Solution

  • I recommend you to use momentjs package for this.

    First run.

    meteor add momentjs:moment
    

    Now you can do the follow

    var day = moment().endOf('day').fromNow(),
        day1 = moment().startOf('day').fromNow();
        console.log("the days ends in " + day)
        console.log("the days starts " + day1)
         if(day === "in 0 hours"){
            alert("The day is over")
            Session.set("dayOver",true)
          }
    
        if(day1 === "in 24 hours ago"){
            alert("The day is over")
            Session.set("dayOver",true)
         }
    

    Here is the JSFiddle

    NOTE: There should be another way, also this take the client time, never trust on the client time, if you want to take the safe time from the server try the mizzao/meteor-timesync package

    Try it

    meteor add mizzao:time sync
    

    and you can do things like.

    var serverTime = TimeSync.serverTime().
    
    serverTime.format(). //for example