1

I'm making a little game out of Javascript, and I'm a little stuck. The game is basically one of those virtual pets, where you have to feed and do stuff with the pet. Now, I have a hunger system made were every 3 seconds the pet gets a little more hungry. The thing is, when I close my laptop, Javascript isn't running. What I'm doing now is getting the date of the last save and getting the difference of that with the current date when I open my laptop.

However, I have no way of triggering the above. I need an event that recognizes when the page comes back up the next time.

If you still don't understand what I'm trying to say, here's an example: I open up a page and I close my laptop. Javascript has stopped. 30 minutes later, I open it back up and now I get an alert with how long I've been gone. What event can trigger the alert?

BTW, I have tried using such things as onload, onpageshow, etc. All of them either trigger once if not at all.

Hutch
  • 63
  • 6
  • See this, might help you...http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled – MixedVeg Dec 02 '14 at 06:37
  • Consider the page visibility API. –  Dec 02 '14 at 06:37
  • And this: http://stackoverflow.com/q/4079115/1355315 – Abhitalks Dec 02 '14 at 06:37
  • And this: http://stackoverflow.com/questions/6346849/what-happens-to-settimeout-when-the-computer-goes-to-sleep – Abhitalks Dec 02 '14 at 06:38
  • And this: https://bitbucket.org/paul.okopny/jquery.wakeup-plugin/wiki/Home – MixedVeg Dec 02 '14 at 06:40
  • 1
    Thanks abhitalks. I'm going to test out this one: http://stackoverflow.com/questions/4079115/can-any-desktop-browsers-detect-when-the-computer-resumes-from-sleep, hopefully it works. :) – Hutch Dec 02 '14 at 06:43

2 Answers2

0

In advice you to put the current date often (like every 2 seconds). And when you load the page, you refer to this data for seeing how long you're been gone.

http://www.w3schools.com/jsref/event_onload.asp

Florian
  • 855
  • 7
  • 12
  • Yeah, I've tried using onload, but it only triggers once. I want it so I can close my laptop and open it up and have it trigger. – Hutch Dec 02 '14 at 06:33
  • ok so you will have to follow several steps. step one save the actual date and then launch a function which will execute itself every 3 seconds. step two, in that function you compare the actual date to the date that is saved and feed your pets with the difference. If you do that, when you will reopen your computer, the difference will be more that the usual 3 second and you can feed your pets in consequence. – Florian Dec 02 '14 at 06:54
0

You do know there is an focus event on the window don't you?

I think it will do what you want

window.addEventListener("focus", function(event) {
    // do your thing
}

check this fiddle for an example http://jsfiddle.net/whQFz/

Sonaryr
  • 1,092
  • 1
  • 10
  • 24
  • I'm aware of it, but that would trigger everytime sometime goes out of the window. It just wouldn't work. – Hutch Dec 02 '14 at 06:37