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

后端 未结 6 675
情话喂你
情话喂你 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:37

    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.

提交回复
热议问题