0

I have created a simple chatroom-like Apache Cordova (HTML) application that allows users to add their own messages with custom usernames in the chatroom.

The messages add up after a while and i have to go into the actual database to delete them every day to avoid clutter.

Is there any way i can create a rule or a javascript function to clear the data at a certain time every day? For instance at 9 am the database would clear all its content from the previous day and night and start the day fresh.

Thanks for the help.

Roger99
  • 981
  • 2
  • 11
  • 42

1 Answers1

2

From the looks of it there are two current similar solutions: Creating Scheduled Task in JavaScript || Later.js execute Function on Schedule?

I am unfamiliar with later.js so I can't help you there.

Using gdoron's code, I believe the code should look similar to this:

function foo(){
    var day =new Date().getDay();
    var hours =new Date().getHours();

    if (day === 0 && hours >12 && hours < 13)  // day is a 0 index base
                                               // sunday between 12:00 and 13:00

//What you want to do goes here
var fredRef = new Firebase('yourDirectoryUrl')
fredRef.remove();

}

setInterval(foo, 3600000); // one hour check.

Notes:

  • Remember to include
    <script src='https://cdn.firebase.com/js/client/2.2.1/firebase.js'></script>
    

  • Also, keep in mind this will clear EVERYTHING.
  • Community
    • 1
    • 1
    Jonathan Lin
    • 124
    • 1
    • 1
    • 11
    • I feel database-clearing should be done from server side, not by invoking from client side. As above solution wont work, if user is offline? – Gopinath Shiva Jul 17 '15 at 13:31
    • Yea it is definitely better done server side. But the question asked for a javascript function as a solution. gdoron mentions in the first link "C#\Java\Ruby\PHP" would probably be better. – Jonathan Lin Jul 17 '15 at 13:33
    • Thanks for the help! the setInterval call will run the foo function every hour? – Roger99 Jul 17 '15 at 14:04
    • @JonathanLin do you know of a way to complete this action on the server side? Is there a rule i can add to my firebase database so that preforms the same function as the method above? – Roger99 Jul 17 '15 at 14:32
    • @DustinHux I'm actually unaware of what kind of schedule they are passing to the setInterval function, but as far as I know, the second argument is the interval in milliseconds. (3,600,000 ms is 1 hour). Also after some small researching, the only way I see to mess with firebase on a server side is to use the REST api. Unfortunately I'm only familiar with firebase' javascript. – Jonathan Lin Jul 17 '15 at 15:34