fuglede
fuglede

Reputation: 18201

Running logic upon progressive web app open

I have an installed progressive web app, i.e. a web page that has been "added to home screen" on a mobile phone/tablet through Firefox, Chrome, or Safari; the question at hand is relevant for all three.

The app relies on data being updated in the background every, say, 5 minutes, through a piece of JavaScript that we'll just refer to as updateData. In a desktop browser, this would work fine through something like

$(document).ready(function() {
    updateData();
    setInterval(updateData, 5*60*1000);
});

but for the progressive web app, this is less than ideal, because a user might receive the update, close the app, only open it again an hour later, and then have to wait five minutes to get the next update.

My question therefore becomes:

Is there a good way to tell if the user just opened the progressive web app, independent of whether it's hosted in Firefox, Chrome, or Safari (or something else?) after a long while and that an update is necessary?

Upvotes: 1

Views: 47

Answers (1)

Medet Tleukabiluly
Medet Tleukabiluly

Reputation: 11930

This will run when tab/window focused, and when browser opened back in mobile

$(window).on('focus', function() { updateData(); });

Upvotes: 2

Related Questions