Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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

about 4 years ago · Juan Pablo Isaza Relatório

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);
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda