How can I count the number of seconds a window has opened?

I am trying to write a script where I need a timer to count the number of seconds of a popup. I'm new to programming, but guess you use javascript for this?

+3
source share
2 answers

Inside the pop-up window, you can use the unloadobject's event windowto detect that the window has closed or go to a new page by pre-registering the time it opened at the top of the document. For instance:

<html>
  <head>
    <script type="text/javascript">
      var start = new Date();

      window.onunload = function() {
        var end = new Date();
        var secondsOpen = Math.floor((end - start) / 1000);
        alert("Pop-up was open for " + secondsOpen + " seconds");
      };
    </script>
  </head>
  <body>
     ...
  </body>
</html>
+5
source

, date, , . :

// Execute this when the popup opens
var popup_opened = (new Date()).getTime();

// And this way you can get the time (in seconds) that the popup has been opened
var current_time = (new Date()).getTime();
var time_spent_opened = (current_time - popup_opened)/100;

, :

function getPopupTime() {
    var current_time = (new Date()).getTime();
    return (current_time - popup_opened)/100;
}
+1

Source: https://habr.com/ru/post/1787227/


All Articles