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

后端 未结 6 662
情话喂你
情话喂你 2020-12-15 21:06

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

6条回答
  •  庸人自扰
    2020-12-15 21:57

    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
    

提交回复
热议问题