2010-02-25 114 views
0

我想在網站上有一個秒錶,它會在標籤上顯示運行時間,而無需重新加載頁面。是否有可能在客戶端做到這一點?我應該使用Ajax計時器還是從.net使用其他的smth?我應該使用Ajax Timer嗎?

網站在C#中。

一些鏈接或演示將非常有幫助!謝謝 !

回答

2

可以使用setTimeout與基本的JavaScript做到這一點:

var totalSeconds = 0 

function stopwatch() { 
    // increment the seconds 
    totalSeconds++; 

    // display the seconds to the user 
    document.getElementById("<%=myLabel.ClientID%>").innerHTML = "You have spent " + totalSeconds + " on this page."; 

    // wait a second and call the timer again 
    setTimeout("stopwatch()", 1000); 
} 

// trigger the timer 
timer(); 

更新:如果用戶要在頁面上一會兒,你可能想顯示一個稍微用戶友好的消息那麼「你在這個頁面上花費了1000秒」。 Here's a quick JavaScript函數會將秒數轉換爲流逝的時間。

1

僅在JavaScript中可以做。這裏有很多例子。只需google them。這是one