2012-06-21 25 views

回答

0

可以通過保存點擊的時間戳和10次點擊前與當前時間戳比較點擊的時間戳實現這一目標:

(我假設你在這裏使用jQuery)

var timestamps = []; 
$('.watched').click(function() { 
    var now = (new Date).getTime(); 
    // add the current timestamp in front of the other timestamps 
    timestamps.unshift(now); 
    if (timestamps.length > 10 && (now - timestamps[10]) < 500) { 
    // do whatever you really wanted to do 
    clickedTenTimes() 
    } 
    // clean unneeded timestamps 
    while (timestamps.length > 10) timestamps.pop() 
}); 
+0

謝謝,這可以按要求工作。但是,它可以通過事件處理程序輕鬆完成。如果可能的話,我想用Rx來處理所有的「時間戳」。 –

+0

直到現在我還沒有使用Rx,但是我會看看。 – Tharabas

0

您可以使用groupByUntil函數在500毫秒內觸發的關鍵事件創建可觀察組。然後只計算每個可觀察組中的事件。如果一個組中有十個或更多的事件,那麼做一些事情。

var keyups = $('#input').keyupAsObservable() 
    .groupByUntil(
     function(x) { return x.currentTarget.id; }, 
     function(x) { return x; }, 
     function(x) { return Rx.Observable.timer(500); } 
); 

keyups.subscribe(function(obs) { 
    obs.count() 
    .filter(function(count) { return count >= 10; }) 
    .subscribe(doSomething) 
}); 
相關問題