0

我正在训练计算打开选项卡直到关闭它的时间我使用了 onload 和 onexit 但它不起作用所以我使用 onload 和 counter 继续计数但输出上没有出现任何内容,如果有人有另一种解决方案请帮忙。

<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>
4

1 回答 1

1

这应该完成它:

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

    window.onbeforeunload = function () {
        var endTime = new Date();
        var timeSpent = (endTime - startTime);
        alert(timeSpent );
    }
</script>
于 2013-10-07T13:46:59.490 回答