Here is a suggestion:
Use the event domready
to record the milliseconds (timestamp) when the page loaded. Then use the event beforeunload
to run code exactly before the page close. It is also possible to play with the focus
in case you want to know when the page is in Focus, or the user is not viewing that page (without Focus).
code:
var aberturaPagina;
$(window).ready(function () {
aberturaPagina = new Date().getTime();
});
$(window).on('beforeunload', function () {
var fechoPagina = new Date().getTime();
var tempoAberto = (fechoPagina - aberturaPagina) / 1000;
// faxer qualquer coisa antes de fechar
});
In my example opens a new window to show seconds. Unlock pop-ups to see the result. This code is an example. You may want to make an AJAX call to register in the database.
It is necessary to know the seconds?
– Paulo
It is necessary yes friend, I do not know the attribute Date very well, but I suppose that if we do not use seconds it will do the updating of the time every 1 minute right? But let’s say the user refreshes the page before completing 1 minute? Then I would end up wasting all this time that has passed..
– Wagner'