统计一段时间内的点击次数

发布于 2024-11-18 21:27:26 字数 73 浏览 3 评论 0原文

如何计算一段时间内的点击次数,例如:用户在 2 秒内点击了多少次?我知道如何计算点击次数,但不知道如何测量时间

谢谢

how can I count the number of clicks in a time interval, for instace: how many clicks the user did in 2 seconds?? I know how to count clicks but I don't know how to measeure time

Thanks

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

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

发布评论

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

评论(4

遥远的她 2024-11-25 21:27:26

尝试使用超时来清除检测点击的功能。使用 jQuery,尝试以下操作:

var clicks=0;
function myClickFunction(event){
    clicks++;
}
$(function(){
    $("#something").bind("click",myClickFunction);
    setTimeout(function(){
        $("#something").unbind("click",myClickFunction);
        alert("You clicked "+clicks+" times.");
    },2000);
});

#something 替换为您想要检测点击的元素的 jQuery 选择器,并将 alert 行替换为您自己将运行的代码定时器用完后。
广告@m

Try using a timeout to clear the function that detects the clicks. With jQuery, try this:

var clicks=0;
function myClickFunction(event){
    clicks++;
}
$(function(){
    $("#something").bind("click",myClickFunction);
    setTimeout(function(){
        $("#something").unbind("click",myClickFunction);
        alert("You clicked "+clicks+" times.");
    },2000);
});

Replace #something with a jQuery selector of an element you want the clicks to be detected on and the alert line with your own code that will be run after the timer has run out.
Ad@m

老娘不死你永远是小三 2024-11-25 21:27:26

HTML:

<button>Click me!</button>
<div id="status">Not Running</div>

JS:

var running = false,
    count = 0,
    run_for = 2000;

var end_counter = function() {
    if (running) {
        running = false;
        $("#status").text("Not Running");
        alert(count);
        started_at = 0;
    }
};
$('button').click(function() {
    if (running) {
        count++;
    } else {
        running = true;
        $("#status").text("Running");
        count = 1;
        setTimeout(end_counter, run_for);
    }
});

演示: http://jsfiddle.net/pXMxQ/2/

HTML:

<button>Click me!</button>
<div id="status">Not Running</div>

JS:

var running = false,
    count = 0,
    run_for = 2000;

var end_counter = function() {
    if (running) {
        running = false;
        $("#status").text("Not Running");
        alert(count);
        started_at = 0;
    }
};
$('button').click(function() {
    if (running) {
        count++;
    } else {
        running = true;
        $("#status").text("Running");
        count = 1;
        setTimeout(end_counter, run_for);
    }
});

Demo: http://jsfiddle.net/pXMxQ/2/

短暂陪伴 2024-11-25 21:27:26
define a variable 
attach click handler to element 
set variable = 0
start ur timer (setTimeout)
increment variable on each click
when timeout handler excutes, detach ur handler

- OR - 

define a variable for counter
define a variable for doCounting
set counter = 0, doCounting = true
start ur timer (setTimeout)
increment counter on each click if doCounting is true
set doCounting = false when timeout handler executes
define a variable 
attach click handler to element 
set variable = 0
start ur timer (setTimeout)
increment variable on each click
when timeout handler excutes, detach ur handler

- OR - 

define a variable for counter
define a variable for doCounting
set counter = 0, doCounting = true
start ur timer (setTimeout)
increment counter on each click if doCounting is true
set doCounting = false when timeout handler executes
公布 2024-11-25 21:27:26

使用 setTimeout 函数,

例如如果每次点击都会增加一个变量点击计数,您可以执行以下操作:

 function startCounting(){
    clickcount = 0;
    setTimeout(function(){alert("you made "+clickcount +" clicks!");}, 2000);
 }

Use the setTimeout function

e.g. if Every click increments a variable clickcount you could do the following:

 function startCounting(){
    clickcount = 0;
    setTimeout(function(){alert("you made "+clickcount +" clicks!");}, 2000);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文