Using a web worker to fire an event on the main thread?

I have a website that periodically logs the user's location using an API navigator.geolocation

that will be used primarily with mobile devices. This requires the screen to be on at all times, and I'm sure you can imagine this isn't ideal. A colleague suggested exploring the use of web workers that can run in the background when the device's screen is off.

Unfortunately, I've since found that web workers don't seem to have access to the full functionality of the API, navigator

and cannot use geolocation. Now I'm wondering if it is possible to do something like periodically calling a function on the main thread from the Web Worker, or periodically triggering an event to call the geolocation function.

I am concerned that if the main thread "falls asleep" (ie the screen is off), it will not listen to workers' triggers. Will it be so? Is it possible to somehow "wake up" on the main thread on demand and perform the function specified by the web worker? Or is there an alternative method to call the geolocation API from a web worker?

+3


source to share





All Articles