offline-timer
v1.0.7
Published
Dead simple plugin showing how long user is offline.
Downloads
6
Readme
Offline timer
Simple plugin for PWA showing how long user is offline (in miliseconds)
Installation
npm install offline-timer --save
API
getTime()
returns:- if user is online -
0
- if user is offline - how long user is offline (or since entering the website if he was offline when entering it) in miliseconds
- if user is online -
notify(intervalInMs)
when user goes offline it emits anofflineTimer
event in a defined interval (e.g. every 2 sec). Sometimes with 1-2 miliseconds delay.
Usage examples
Get the information how long user was offline after coming back online:
import offlineTimer from 'offline-timer'
window.addEventListener('online', function () {
console.log(offlineTimer.getTime())
})
Shows an alert after user is offline for 5 sec:
import offlineTimer from 'offline-timer'
const interval = 5000
offlineTimer.notify(interval)
window.addEventListener('offlineTimer', function () {
const timeOffline = offlineTimer.getTime()
if (timeOffline >= interval && timeOffline < 2*interval) {
alert("You are offline for 5 seconds!")
}
})