2013-10-07 30 views
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> 

回答

1

這應該完成它:

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

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

感謝Raidri您的快速的答案,但我想這和它仍然是因此未給您打開水龍頭直至收盤它的時間。 –

+0

適用於Firefox和IE。你在控制檯中遇到錯誤嗎? – Raidri

相關問題