如何计算时间间隔内的点击次数,例如:用户在2秒内完成的点击次数?我知道如何计算点击次数,但我不知道如何衡量时间
由于
答案 0 :(得分:1)
尝试使用超时清除检测点击的功能。使用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
行替换为您自己的代码,该代码将在计时器用完后运行。
广告@米
答案 1 :(得分:1)
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);
}
});
答案 2 :(得分:0)
使用setTimeout函数
e.g。如果每次点击都会增加一个变量clickcount,您可以执行以下操作:
function startCounting(){
clickcount = 0;
setTimeout(function(){alert("you made "+clickcount +" clicks!");}, 2000);
}
答案 3 :(得分:0)
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