Using a Web Worker to trigger an event in the main thread?

Viewed 322

I have a website which periodically logs the user's location using the navigator.geolocation API, to be used primarily with mobile devices. This requires that the screen is always on, which I'm sure you can imagine is not ideal. A colleague suggested looking into using Web Workers, which can run in the background while a device's screen is off.

Unfortunately, I've since discovered that web workers do not seem to have access to the full functionality of the navigator API, and cannot use geolocation. I'm now wondering if it might be possible to do something like periodically calling a function in the main thread from the Web Worker, or periodically triggering an event to call a geolocation function.

My concern is that if the main thread is 'asleep' (i.e. the screen is off) then it won't be listening for the workers' triggers. Would that be the case? Is it possible to somehow 'wake up' the main thread on demand and have it run a function as specified by the web worker? Or is there some alternative method to calling the geolocation API from a Web Worker?

0 Answers
Related