Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!