Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

65
Vistas
Angular how to track/check time before browser reload/refresh

On my Angular web-app, when a browser refreshes or reloads, the login for the user is lost and he must go through the login steps again. I would like to allow the login status remain open for the user after the browser reload, but only within a short interval, perhaps 10 seconds or so. When the web-app reloads, it checks if the come-back is within these 10 seconds interval. For that I need to know when the refresh/reload or the last moment the app was active.

How do we determine the moment/time right before the browser reloads (or closes) or the nearest time to that?

5 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can capture the reload event and store a timestamp to the localstorage, then do check and comparison each time your app is initiated. A simple function can be:

window.onbeforeunload = ()=>{
localStorage.setItem('last_reload_time',(new Date()).getTime());
}

Then in your app, check for last_reload_time and do compare with current timestamp.

Another DOM event that may help is visibilitychange

5 months ago · Juan Pablo Isaza Denunciar

0

In its simple JS form, I used the answer by Metabolic as the starting point. However, the functionality of the event: "onbeforeunload" is a bit tricky as stated here: MDN, and few browsers, e.g. Chrome ware giving me cold shoulder on the event - not firing. Note, that in most cases the reload event fires, but is not caught by the debugger and if you'll place breakpoints in (eg: in fn: onBeforeUnload() ), do not expect them to stop your code on the event!

I used this approach with rxjs to resolve - on Angular.

 import { fromEvent } from 'rxjs';

 persistKey: string = 'TIME_BEFORE_UNLOAD';

                        //// eventually, instead of rxjs: fromEvent(...)
                        //// you can use this:
  // @HostListener("window:beforeunload", ["$event"])
  // unloadHandler(event: Event) {
  //    this.onBeforeUnload(event);
  // }


 ngOnInit() {
                        // use this to test and see;
                        // the time stamps should change in console
                        // after each reload click
      console.log('-- Stored time before unload: ',  
                      localStorage.getItem(this.persistKey));

      this.subscribeToBrowserEvents();
 }

 private subscribeToBrowserEvents() {
    fromEvent(window, 'beforeunload')
    .subscribe(event => this.onBeforeUnload(event));
 }

 private onBeforeUnload(event) {
    const val: string = new Date().toISOString();
    localStorage.setItem(this.persistKey, val);
 }
5 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos