跟踪在页面上显示某些元素需要多长时间 [英] Keep track of how much time is spent showing certain elements on the page

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

问题描述



我想计算一下,如果我们有4个div(3隐藏,1可见),用户可以通过javascript / jQuery在它们之间切换。花费在每个Div上的时间,并将包含该时间的xhr发送到服务器以将其存储在数据库中。这个xhr将在用户切换div视图时发送。



我该怎么做?任何提示都将不胜感激。



谢谢,

解决方案

任何时候,你都可以在一个变量中记录一个开始/一圈时间:

  var start = new Date(); 

如果要计算已用时间,只需从新的Date实例中减去存储的日期: / p>

  var elapsed = new Date() -  start; 

这会给你以毫秒为单位的经过时间。做额外的数学(除法)来计算秒数,分钟数等等。


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,

解决方案

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.

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

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