跟踪在页面上显示某些元素所花费的时间 [英] Keep track of how much time is spent showing certain elements on the page

查看:19
本文介绍了跟踪在页面上显示某些元素所花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有 4 个 Div(3 个隐藏,1 个可见),用户可以通过 javascript/jQuery 在它们之间切换.

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

我想计算在每个 Div 上花费的时间,并将包含该时间的 xhr 发送到服务器以将其存储在数据库中.当用户切换 div 视图时将发送此 xhr.

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.

谢谢,

推荐答案

在任何时候,您都可以通过以下方式在变量中记录开始/单圈时间:

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

var start = new Date();

当你想计算经过的时间时,只需从一个新的 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.

这篇关于跟踪在页面上显示某些元素所花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆