Beforeunload executing business logic before leaving the page

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.