Currently, I have implemented a function within a timed loop to monitor if a user goes offline:
created() {
setInterval(() => {
this.checkOnline();
}, 30000);
}
Function:
checkOnline(){
this.status = navigator.onLine ? true : false;
}
Is there a more efficient way to detect online status without relying on a timer?
Additionally...
I want to display a brief alert message when the user comes back online. The idea is to hide a div element when the user is online or offline. When transitioning from offline to online, briefly show the div for about one second and then hide it again. I've attempted using setTimeout and watchers but haven't achieved the desired result.
Edit:
One approach that partially addresses this issue is:
data() {
return {
status: navigator.onLine ? true : false,
}
}
Followed by a watcher to handle the back-online notification:
watch: {
status: function (val) {
if(this.status === true){
this.showAlert = true;
setTimeout(()=>{ this.showAlert = false; }, 1000);
}
},
},
Are there alternative methods to achieve this functionality other than utilizing a watcher or a dedicated notify plugin?