Window has a Beforeunload event, which is triggered in front of the left page, and you will soon be reminded of the unload of the event, the difference between the two is quite large, literally very good understanding, beforeunload on the timeline before unload execution, The decomposition point is the page unloading time;
There are often some applications that perform some business in front of the user leaving the page, all of which use the onbeforeunload event, such as recording a user's long-stay business, which is included in the application for page access statistics such as GA:
1;(function(){2 varStartTime = Math.ceil (NewDate (). GetTime ()/1000),//units per second3Getduration =function(){4 varTime = ",5Hours = 0,6minutes = 0,7seconds = 0,8EndTime = Math.ceil (NewDate (). GetTime ()/1000),9Duration = EndTime-StartTime;Ten OneHours = Math.floor (duration/3600); Number of stay hours Aminutes = Math.floor (DURATION%3600/60); Number of minutes to stay -seconds = Math.floor (duration%3600%60);//number of seconds to stay - theTime = (Hours < 10?) ' 0 ' + hours:hours + ': ' + (minutes < 10?) ' 0 ' + minutes:minutes + ': ' + (seconds < 10?) ' 0 ' +seconds:seconds); - - returnTime ; - }; + - +Window.onbeforeunload =function(e) { A varDuration =getduration (); at - //request (duration); - }; -})();
Beforeunload executing business logic before leaving the page