Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep track of how much time is spent showing certain elements on the page

So lets say we have 4 Divs (3 hidden, 1 visible), the user is able to toggle between them through javascript/jQuery.

I want to calculate time spent on each Div, and send an xhr containing that time to server to store it in the database. This xhr will be sent when the user toggle the div view.

How can I do that? Any hints will be greatly appreciated.

Thanks,

like image 310
wael34218 Avatar asked Jun 14 '11 09:06

wael34218


People also ask

How to measure a time spent on a page JavaScript?

Instead, use performance. now() which shows how many milliseconds have elapsed since the user first navigated to the page. Date. now , however, measures clock-time which can differ from navigation-time by a second or more due to factors such as Time resynchonization and leap seconds.


3 Answers

At any point, you can record a a start/lap time in a variable with:

var start = new Date();

When you want to calculate the elapsed time, simply subtract the stored date from a new Date instance:

var elapsed = new Date() - start;

This will give you the elapsed time in milliseconds. Do additional math (division) to calculate seconds, minutes, etc.

like image 182
Ates Goral Avatar answered Oct 11 '22 01:10

Ates Goral


Here you go:

HTML:

<div id="divs">
    <div>First</div>
    <div class="selected">Second</div>
    <div>Third</div>
    <div>Fourth</div>
</div>

<p id="output"></p>

JavaScript:

var divs = $('#divs > div'),
    output = $('#output'),
    tarr = [0, 0, 0, 0],
    delay = 100;

divs.click(function() {
    $(this).addClass('selected').siblings().removeClass('selected');
});

setInterval(function() {
    var idx = divs.filter('.selected').index();
    tarr[idx] = tarr[idx] + delay;
    output.text('Times (in ms): ' + tarr);
}, delay);

Live demo: http://jsfiddle.net/7svZr/2/

I keep the times in milliseconds because integers are cleaner and safer (0.1 + 0.2 != 0.3). Note that you can adjust the "precision" (the delay of the interval function) by setting the delay variable.

like image 21
Šime Vidas Avatar answered Oct 10 '22 23:10

Šime Vidas


Here is a reusable class, example is included in code:

/*
     Help track time lapse - tells you the time difference between each "check()" and since the "start()"

 */
var TimeCapture = function () {
    var start = new Date().getTime();
    var last = start;
    var now = start;
    this.start = function () {
        start = new Date().getTime();
    };
    this.check = function (message) {
        now = (new Date().getTime());
        console.log(message, 'START:', now - start, 'LAST:', now - last);
        last = now;
    };
};

//Example:
var time = new TimeCapture();
//begin tracking time
time.start();
//...do stuff
time.check('say something here')//look at your console for output
//..do more stuff
time.check('say something else')//look at your console for output
//..do more stuff
time.check('say something else one more time')//look at your console for output
like image 5
Shawn Dotey Avatar answered Oct 11 '22 01:10

Shawn Dotey