2011-07-05 3 views
2

시간 간격으로 클릭 수를 계산하려면 어떻게해야합니까? 사용자가 2 초 동안 클릭 한 횟수는 몇 번입니까? 내가 클릭 수를 계산하는 방법을 알고 있지만 나는 시간을시간 간격으로 클릭 수를 계산하십시오.

감사

+0

당신이 어떠한 클릭을 참조하거나 클릭이 특정 요소에 발생해야합니까 있습니까 ? – jerluc

답변

0

사용의 setTimeout 기능

예를 measeure하는 방법을 모른다 모든 클릭이 증가하는 경우 변수는 다음을 수행 할 수 clickCount는 :

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

startCounting에서 clickcount = 0으로 설정하십시오. – hungryMind

+0

@ hungrymind - 좋은 지적입니다! :) – BonyT

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 
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); 
}); 

당신이 클릭가 감지 할 요소의 jQuery를 선택하고 타이머가 밖으로 실행 된 후 실행됩니다 자신의 코드로 alert 라인 #something를 교체합니다. 광고 @ m에

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); 
    } 
}); 

데모 : http://jsfiddle.net/pXMxQ/2/

관련 문제