0

So I'm making a web app that is sort of like adVenture Capitalist. My question is: is there a way to have a function continuously running even when I close the webpage? For example, let's say, I have 5 coins and there is a function that adds 5 coins every hour. I exit the page (close the browser) and return to that page 5 hours later. There would be 30 coins total from my initial coin value of 5 plus the 25 from the five hours.

I only know Javascript, HTML, PHP, and jQuery to some extent, so if you can please limit it to those languages. However if there are better or faster solutions using other languages please let me know. Thanks everyone :)

Ramenous
  • 218
  • 1
  • 8
  • 6
    store the actual time in localStorage and then on reload calculate how long it's been since you stored it – Kaiido Aug 17 '15 at 05:24
  • 2
    Javascript runs in the browser so there is no way you can do it in javascript, you've to do it in server side. – seenukarthi Aug 17 '15 at 05:24
  • @Kaiido will using localStorage is reliable? What if the user clears the browser data? – seenukarthi Aug 17 '15 at 05:26
  • @KarthikeyanVaithilingam it depends of the device, some mobile ones (ios) does clear the localStorage on app shutdown. Desktop ones won't, except if you specifically clear the localStorage. But of course you could store it to server too – Kaiido Aug 17 '15 at 05:29

3 Answers3

1

You can do it using a third-party scheduling service. If you can make external HTTP requests, I recommend using Cron from SaturnAPI. It is a RESTful API for you to schedule callbacks to your domain like so

curl -X POST \
  https://saturnapi.com/cron/jobs \
    -H saturnapi-access-key:'YOUR_SATURNAPI_ACCESS_KEY' \
    -d dateTimeUTC='2015:09:25 14:30' \
    -d dateTimeFormat='YYYY:MM:DD HH:mm' \
    -d callbackURL='https://your.verified.domain.com' \

You will have to sign up to get an API access key and also verify your callback domain. It accepts a variety of time formats according to moment.js.

For PHP, there are many options to make HTTP POST requests, such as this, this and this.

NOTE: This method will not block code on the server or the client, which I think is best. To make cron jobs recurring, simply make another HTTP request when the callback occurs. Cron jobs can also be cancelled before executing as shown below.

curl -X DELETE \
  https://saturnapi.com/cron/jobs/YOUR_JOB_ID \
    -H saturnapi-access-key:'YOUR_SATURNAPI_ACCESS_KEY' \

Disclosure: I work for SaturnAPI

Community
  • 1
  • 1
FullStack
  • 5,902
  • 4
  • 43
  • 77
  • but sheduling cron**s** (there will probably be several users of this webapp) just to increment a timer is probably not the best – Kaiido Aug 17 '15 at 05:43
  • @Kaiido cron jobs on SaturnAPI are not recurring. They execute once and that's it. They can also be cancelled before executing, so this is one possible solution for OP. I will clarify my answer – FullStack Aug 17 '15 at 06:49
  • Your edit doesn't help understand how a cron will help OP. What do you mean by "they're not recurring"? If you set a cron to fire only once, it's just a delayed script. But how can this help OP? He just need to check how long it is since the last time he incremented its `per hour points` counter. Your solution would only work if the cron was recurring, every hour, to update the points counter on server, but then it would need as many crons as there are users on this webapp. Just save once the lastUpdatedTime and do the math on reload. No need for your awesome API here. – Kaiido Aug 17 '15 at 06:58
  • I clarified on how to make it recurring. The problem with waiting for reload is that you assume the client will reload. – FullStack Aug 17 '15 at 08:05
  • But if he doesn't, you don't need to increment its score :P – Kaiido Aug 17 '15 at 08:07
  • What if you need the score to do something before the reload? – FullStack Aug 17 '15 at 08:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87141/discussion-between-kaiido-and-fullstack). – Kaiido Aug 17 '15 at 08:08
  • Sorry if I wasn't clear guys. It's not so much of a timer in a way. To put it simply, I just want to know whether it is possible to have a user close the browser and still have his or her score increase. I have come to realize it that it's more of a server side task because when the client page is closed, it's done. However, the server will still be running. So is there a way to perform this. Thanks – Ramenous Aug 17 '15 at 22:58
  • @FullStack Why should you do something that the user cannot see? – Frederik.L Aug 18 '15 at 01:29
  • @Frederik.L because other users might need to see it, some calculations need to be performed on the server, or calculations need to be performed continuously. Consider a leaderboard for example – FullStack Aug 18 '15 at 04:39
1

There is no need to keep a timer running if no user actions are taken (i.e.: when the user isn't even on the page anymore). In case you have some random factors in the timer, just do it X times when the user comes back.

To keep things simple, only one information is needed to be persistent : the score and full date of the last "page unload". It could be saved in local storage given that appropriate feature testing is done, but safer in server-side. For a desktop web app, I'd prefer server-side storage since there is many users that are deleting local files on a regular basis.

I hope this helps and good luck!

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
0

Well there is what fullstack said as well as running a php script with ignore user abort set to false. So long as the file is looping, it should in all intensive purposes keep running.

Source

Community
  • 1
  • 1
Jorden
  • 99
  • 6