0

I've looked all over the net for a solution to the following problem: how do I create a timer that works in an inactive tab on any modern web browser?

Solutions appear to exist using JavaScript (using setInterval / setTimeout and using a Date object / timestamp), but these require the user to refocus the window to reload the timer. What if the timer finishes and the window is out of focus? I need the timer to make a noise or alert the user in some way, so these solutions do not apply.

I'm asking for a solution using any server-side or client-side language.

gilbert-v
  • 1,263
  • 1
  • 11
  • 23
  • it would certainly depend on the javascript engine / browser ... – jeremy Jan 04 '18 at 21:33
  • If it helps, this is an app that will exclusively be available on Google Chrome. V8 engine. – gilbert-v Jan 04 '18 at 21:34
  • then I'm not sure what the issue is with "requiring the user to refocus." [this](https://jsfiddle.net/4y9pxpdn/) seems to work fine when unfocusing – jeremy Jan 04 '18 at 21:41
  • Try using Chrome. Go into an empty tab and enter into the console `setTimeout(alert, 10000)`. Press `Enter` and exit the tab. Watch a clock for 10 seconds. The alert will pop up around 15 - 20 seconds later rather than 10. I need accuracy for a timer. – gilbert-v Jan 04 '18 at 21:55
  • hmm.... seems to be accurate for me :/. this is why i say: it certainly depends – jeremy Jan 04 '18 at 22:04
  • perhaps relevant https://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs – jeremy Jan 04 '18 at 22:28
  • perhaps a solution: https://github.com/turuslan/HackTimer – jeremy Jan 04 '18 at 22:29

1 Answers1

0

setInterval and setTimeout should work even if tab is not in focus (as any other actions).

Try to write this in console of new tab and switch to another tab:

setTimeout(function(){alert('hello')}, 5000)

After 5 seconds some kind of mark would appear on tab. This is how it looks in firefox: screenshot

aiven
  • 3,775
  • 3
  • 27
  • 52
  • This may work in Firefox, but not in all modern browsers and certainly not Google Chrome. – gilbert-v Jan 04 '18 at 21:55
  • I'm pretty sure that firefox is also a modern browser :D Just tried my "trick" in chrome and it didn't mark tab, but jump right into that tab (and focused it). Maybe you have severe restrictions in chrome that disallow such actions? – aiven Jan 04 '18 at 22:00
  • Note *all*. Firefox is *a* modern browser. Chrome does "jump right into" the tab when `alert` is called, but it jumps in later than expected due to unfocused tabs not being processed as a priority. – gilbert-v Jan 04 '18 at 22:03
  • 2
    Yes, it looks like alert delayed a little (1 second in my case). But I think that this is happening because of browser optimization. It treat focused tab as main process and for other tabs gives fewer level of priority. And I think that you **can't** just ask browser to make your tab "a big deal" because everyone would do that. – aiven Jan 04 '18 at 22:12
  • Google does it though, so it must be possible. https://www.google.co.uk/search?q=timer+50+seconds&oq=timer+50+seconds&aqs=chrome..69i57j0l5.3655j0j7&sourceid=chrome&ie=UTF-8 – gilbert-v Jan 05 '18 at 10:05
  • Google literally owns Chrome and can do whatever he wants :D In firefox google timer has some delay. And I also tried first timer from search result (online-stopwatch.com/timer) and it also has about 1 second delay. – aiven Jan 05 '18 at 11:05