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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eae425-47f78.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eae425-47f78.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?