简体   繁体   中英

calculate the time from opening the tab until closing it

I am traing to calculate the time when I open the tab until I close it I used onload and onexit but it didnot work so I used onload and counter to keep counting but nothing appear on output, if any one have another solution please help .

<html>
    <head>
        <script type="text/javascript">
            var startTime = new Date();        //Start the clock!
            window.onbeforeunload = function ()        //When the user leaves the page(closes thewindow/tab, clicks a link)...
            {
                var endTime = new Date();        //Get the current time.
                var timeSpent = (endTime - startTime);        //Find out how long it's been.
            }
            function timedCount() {
                var c = timeSpent + 1;
                t = setTimeout("timedCount()", 1000);
                alert(c);        //Pop up a window with the time spent in microseconds.
            }

        </script>
    </head>
    <body>
    </body>
</html>

This should get it done:

<script type="text/javascript">
    var startTime = new Date();

    window.onbeforeunload = function () {
        var endTime = new Date();
        var timeSpent = (endTime - startTime);
        alert(timeSpent );
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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