Should I use Ajax Timer?
I want to have a stopwatch on the site, which displays running time on the label without reloading a page. Is it possible to do this on client side? Should I use Ajax timer or smth else f开发者_C百科rom .net?
Website is in C#.
Some links or demos would be really helpful ! Thanks !
You can do this with basic JavaScript using setTimeout:
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();
Update: If the user is going to be on the page for a while you probably want to display a slightly more user-friendly message then "You have spent 1000 seconds on this page". Here's a quick JavaScript function that'll transform the seconds into time elapsed.
It is possible to do in javascript only. There are so many example out there. Just google them. Here's one.
精彩评论