简体   繁体   English

Angular 6:尝试对浏览器关闭事件(浏览器的卸载/卸载事件之前)进行同步调用(删除)

[英]Angular 6: Trying to make synchronous call (delete) on browser close event (beforeunload/unload event of browser)

Actually, I want to update a flag in Db using a service call(Delete method) once the user close the browser.实际上,我想在用户关闭浏览器后使用服务调用(Delete 方法)更新 Db 中的标志。 I am able to detect browser close action using onbeforeunload and onunload events but async call doesn't work for me(sometimes in debugging mode it works fine but on higher env it doesn't work).我能够使用onbeforeunloadonunload事件检测浏览器关闭操作,但异步调用对我不起作用(有时在调试模式下它工作正常,但在更高的环境下它不起作用)。

Then, I tried to make sync request but then I found that Chrome now disallows synchronous XHR during page dismissal when the page is being navigated away from or closed by the user.然后,我尝试发出同步请求,但后来我发现当页面被用户导航离开或关闭时,Chrome 现在在页面关闭期间不允许同步 XHR。 check link : https://www.chromestatus.com/feature/4664843055398912检查链接: https : //www.chromestatus.com/feature/4664843055398912

I have tried new XMLHttpRequest() as sync, fetch api also Navigator.sendBeacon() but unfortunately nothing works for me.我已经尝试过 new XMLHttpRequest()作为同步,也获取 api Navigator.sendBeacon()但不幸的是没有任何东西对我有用。

Please suggest something which works because I have visited so many posts but nothing works for me.请提出一些有效的建议,因为我访问了很多帖子,但没有任何内容对我有用。

Thanks in advance.提前致谢。

I have some solution for this.我对此有一些解决方案。 Hope so any one of them solves your issue.希望他们中的任何一个都能解决您的问题。

constructor() {
    window.onbeforeunload = ()=>{
      //call API here
    }
  }

In your component constructor write above code在您的组件构造函数中编写上面的代码

OR或者

In my opinion the better idea is making the heartbeat api that sends requests every N seconds to notify server that the session is active and the user is online.在我看来,更好的想法是让心跳 api 每 N 秒发送一次请求,以通知服务器会话处于活动状态并且用户在线。 On the server check every M minutes if there was no heartbeat requests for more than N seconds: if it is so - execute the API request(what you wanted to execute on crash).在服务器上每 M 分钟检查一次是否有超过 N 秒的心跳请求:如果是这样 - 执行 API 请求(您想在崩溃时执行的请求)。

OR或者

'beforeunload' would be trigger when refreshing pages, closing tab, or closing the browser. 'beforeunload' 将在刷新页面、关闭选项卡或关闭浏览器时触发。

  @HostListener('window:beforeunload', ['$event'])
    beforeUnload(e: Event) {
        e.returnValue = false;
    }

OR或者

It is not possible to ensure that every time an user exits a browser page a specific function will be triggered.无法确保每次用户退出浏览器页面时都会触发特定功能。 The reason is that the browser could close the window for many reasons.原因是浏览器可能出于多种原因关闭窗口。 Yes it could be a user action but this is not the only case.是的,这可能是用户操作,但这不是唯一的情况。 For example The browser could crash.例如浏览器可能会崩溃。

In my case I will have to find another strategy to track the time the user stays on the page.就我而言,我必须找到另一种策略来跟踪用户停留在页面上的时间。 For example I am planning to send a lot of API calls before the user exits with all the informations I need to understand his stay on the page.例如,我计划在用户退出之前发送大量 API 调用,并提供我需要了解他在页面上停留的所有信息。 I will update the answer when I will reach a good solution.当我找到一个好的解决方案时,我会更新答案。 Anyway I will still wait for better answers.无论如何,我仍然会等待更好的答案。

You can use the fetch API.您可以使用获取 API。 The syntax would be:语法是:

fetch('API', {
    method: 'POST', // Other opn are also supported like GET,PUT DELETE
    body: '',
    keepalive: true
});

Just an additional read: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API只需额外阅读: https : //developer.mozilla.org/en-US/docs/Web/API/Fetch_API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM