微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何知道我是否关闭了一个选项卡、最后一个选项卡或 Angular 中的浏览器

如何解决如何知道我是否关闭了一个选项卡、最后一个选项卡或 Angular 中的浏览器

问题如下:当我关闭打开的最后一个选项卡或关闭浏览器

时,我想清除我的localStorage(我的会话)

解决方法

我是这样解决的: 我将在重新加载页面时清理会话,并且从上次打开的选项卡到现在已经过去了 80 秒。或者,如果我有多个选项卡打开并关闭浏览器。这是我将根据时间验证进行的。

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss']
})
export class AppComponent {
  openTabs: number;

  constructor(private authenticationService: AuthenticationService) {
    window.onload = () => {
      localStorage.setItem('onloadTime',new Date().getTime().toString()); //I save the moment when i am reloading
      this.openTabs = parseInt(localStorage.getItem('openTabs')) || 0; //I check if there is some tab open and if not start at 0
      localStorage.setItem('openTabs',(++this.openTabs).toString());//now the is 1 more tab open,so i saved

      if (this.accessByTime() && parseInt(localStorage.getItem('openTabs')) == 1) { // Check if 80 seconds have passed since I close the last tab. If true i clean the session
        localStorage.removeItem('storagetime');
        this.authenticationService.logout();
      }
    }

    window.onbeforeunload = () => {      
      let localStorageTime = parseInt(localStorage.getItem('storagetime'))
      localStorage.setItem('onbeforeunloadTime',new Date().getTime().toString()); //Saving the time before close a tab or browser
      if (localStorageTime != null && localStorageTime != undefined) { //if localStorageTime exits i going to check if was closed a tab or browser
        let currentTime = new Date().getTime(),timeDifference = currentTime - localStorageTime;
        localStorage.setItem('timeDifference',timeDifference.toString());

        if (timeDifference < 65) { // if i have more than one tab and close the browser there is little difference lapse between close one and another this lapse will be my validation
          //Browser Closed
          localStorage.removeItem('storagetime');
          localStorage.removeItem('openTabs');
          this.authenticationService.logout();
        } else { // a simple tab was closed so i save the current open tabs number and the time
          this.openTabs = parseInt(localStorage.getItem('openTabs')) || 0;
          if (this.openTabs > 0) localStorage.setItem('openTabs',(--this.openTabs).toString());
          localStorage.setItem('storagetime',new Date().getTime().toString()); // i save storage time to future validations
        }


      } else { //if there is not any tab closed previously I save the time of a new
        localStorage.setItem('storagetime',new Date().getTime().toString());
      }
    };
    
  }

  accessByTime(): boolean { //function to validate difference between the last closed an the new open
    let difference = parseInt(localStorage.getItem('onloadTime')) - 
    parseInt(localStorage.getItem('onbeforeunloadTime'))
    localStorage.setItem('difference',difference.toString());

    if (difference < 80 * 1000) {
      return false;
    }
    return true;
  }



}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。