跟踪在页面上显示某些元素所花费的时间

编程入门 行业动态 更新时间:2024-10-26 03:34:15
本文介绍了跟踪在页面上显示某些元素所花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-28 18:21:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/736966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   页面   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!