Vue snooping closes the page sends an axios request before closing the page how to send the request first and then close the page

the problem encountered now is that the axios request can not be submitted when the page is closed. Is there any solution?

        window.addEventListener("beforeunload", function() {
      beforeUnloadTimestamp = new Date().getTime();
  })

  window.addEventListener("unload", function() {
    beforeUnloadTimestamp = beforeUnloadTimestamp || 0;
    localStorage.setItem("onload", new Date().getTime());
    // chromeie11 chromeIE11 //  chrome >=11ms ie11 >=2ms  chrome >=2msIE11 >=3000ms
    if (isIE) {
      if (new Date().getTime() - beforeUnloadTimestamp > 500) {
        isClose = true;
      }
    } else {
      if (new Date().getTime() - beforeUnloadTimestamp < 5) {
        isClose = true;
      }
    }
    
    if (isClose) {
      axios.get("/au/logout").then(res => {
        window.sessionStorage.clear();
      });
      localStorage.setItem("close", new Date().getTime());
    }else{
      localStorage.setItem("refresh", new Date().getTime());
    }
  })
Mar.14,2021

none. The request is asynchronous and unload will not wait for your request to return.


you can change the ajax request to Synchronize request, and then close the page after the request returns

Menu