2012-02-14 42 views
0

我的目標是一次多長時間用戶通過一堆頁面瀏覽(單頁上隱藏層)上所以我可以把時間記錄在mm:ss或者sss中。如何使用jQuery一次的行動之間需要多久的頁面(點擊按鈕之間秒錶)

我會從一個輸入按鈕,這應該啓動計時器開始,然後在上的隱藏的div 1另一個按鈕,點擊它會停止計時器。

結束目標將是具有相同的第3個定時器和它必須通過jQuery否則瓦特/電子之間可以被點擊2個不同的按鈕被用於時間。

我知道如何能在控制器來完成,但我需要收集3周獨立的時間和不能擁有它被超過3個獨立的動作完成/ DB撲救。

到目前爲止,所有我發現是參考你在已經設置就可以了一段時間,然後充當倒計時,而不是一個秒錶。

任何幫助將不勝感激。

回答

1
$(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

1

你不需要jQuery這一點。你可以得到當前毫秒,因爲最大紀元值是這樣的:

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

或者,如果字符計數是你的事:

var ms = +new Date(); 

只是做了兩遍,減去第二個第一。唯一的地方jQuery的可能參與將是對按鈕掛鉤點擊,例如:

$("selector_for_button").click(function() { 
    // ...your code here... 
}); 
相關問題