How to execute a function once in a day?

Hi I am creating an ionic app in which I want to show number of days.
ex: If user is installed app today and opens on first day it will show 1 and opens on 10th day it should show 10.
For that I need to execute function once every 24 hours.I tried setInterval in angularJs but it is not working when we close the app(I mean kill the app) .
Help me in this issue. I am learning ionic and angularJs everyday Please bear with me If I am asking a silly question

A much better way to do this would be to just store the date they first open the app, and then every time they visit the app grab the current date and work out the difference compared to the original date.

you can use the Javascript Date.now() function to get the amount of seconds that past since jan 1 1970 00:00 UTC.
http://www.unixtimestamp.com/

Save this value on first run some persistent memory.
ex. https://blog.nraboy.com/2014/12/use-ngstorage-angularjs-local-storage-needs/

every time afterwads when you open the app you compare the current value of your Date.now() with the stored value by distracting the stored value from the current value.Then you divide it by 86.400(seconds) and you have your days past since installation.

Some pseudo-code:

var now=Date.now(); var storage=$localStorage; var checkDate=function(){ if(storage.initDate){ $scope.daysPast=Math.floor(now-storage.initDate); }else{ storage.initDate=now; } }

Hope it helps.
Keep on hacking away…