如何使用jquery来计算页面上的操作之间需要多长时间(按钮单击之间的秒表)

发布于 2025-01-05 01:17:32 字数 340 浏览 0 评论 0原文

我的目标是计算用户浏览一堆页面(单个页面上的隐藏 div)所需的时间,以便我可以记录最好以 mm:ss 或仅 sss 为单位的时间。

我将从一个输入按钮开始,该按钮应该启动计时器,然后单击其中一个隐藏 div 上的另一个按钮,它将停止计时器。

最终目标是在同一页面上有 3 个计时器,并且必须通过 jquery 或 w/e 否则可以用于在单击的 2 个不同按钮之间进行计时。

我知道如何在控制器中完成它,但因为我需要收集 3 次单独的时间,并且不能通过 3 次单独的操作/数据库保存来完成它。

到目前为止,我发现的只是您已经设置了时间,然后它充当倒计时而不是秒表。

任何帮助将不胜感激。

My aim is to time how long it take a user to navigate through a bunch of pages(hidden divs on single page) so I can record time taken preferably in mm:ss or just sss.

I would start from a input button which should start the timer and then upon click of another button on 1 of the hidden divs it will stop the timer.

End goal would be to have 3 timers on the same page and it has to be via jquery or w/e else can be used to time between 2 different buttons being clicked.

I know how it can be done in the controller but as I need to collect 3 separate times and can't have it being done over 3 separate actions/db saves.

So far all I've found is reference to you having already set a time on it and then that acts as a countdown rather than a stopwatch.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2025-01-12 01:17:32

为此,您不需要 jQuery。您可以像这样获取当前自 The-Epoch 以来的毫秒值:

var ms = new Date().getTime();

或者,如果您喜欢字符计数:

var ms = +new Date();

只需执行两次,然后从第二次中减去第一次。唯一可能涉及 jQuery 的地方是挂钩按钮上的点击,例如:

$("selector_for_button").click(function() {
    // ...your code here...
});

You don't need jQuery for this. You can get the current milliseconds-since-The-Epoch value like this:

var ms = new Date().getTime();

Or if character-counting is your thing:

var ms = +new Date();

Just do it twice and subtract the first from the second. The only place jQuery might be involved would be hooking the clicks on the buttons, e.g.:

$("selector_for_button").click(function() {
    // ...your code here...
});
无悔心 2025-01-12 01:17:32
$(function () {

    //cache the time when `document.ready` fires and create a variable to track clicks
    var startTime = new Date().getTime(),
        clicks    = [];

    //only bind the `click` event handler to elements with the `my-links` class so we don't track every click on the webpage
    $('.my-links').on('click', function () {

        //push an object onto the tracking array that includes the target of the link and the time when it was clicked
        clicks.push({ time : new Date().getTime(), target : $(this).attr('href') });
    });
});​

这是一个演示: http://jsfiddle.net/wwnyY/1/

然后你可以迭代您保存的点击数组,或将其发送到服务器端脚本以存储以供以后使用。

以下是 Date() 的一些不错的文档:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

$(function () {

    //cache the time when `document.ready` fires and create a variable to track clicks
    var startTime = new Date().getTime(),
        clicks    = [];

    //only bind the `click` event handler to elements with the `my-links` class so we don't track every click on the webpage
    $('.my-links').on('click', function () {

        //push an object onto the tracking array that includes the target of the link and the time when it was clicked
        clicks.push({ time : new Date().getTime(), target : $(this).attr('href') });
    });
});​

Here is a demo: http://jsfiddle.net/wwnyY/1/

You can then iterate through your array of saved clicks, or send it to a server-side script to store for later use.

Here are some good docs for Date(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文