【发布时间】:2010-02-25 19:30:14
【问题描述】:
我想在网站上有一个秒表,它在标签上显示运行时间而不重新加载页面。是否可以在客户端执行此操作?我应该使用 Ajax 计时器还是 .net 中的其他东西?
网站使用 C#。
一些链接或演示会很有帮助!谢谢!
【问题讨论】:
标签: ajax asp.net-ajax timer stopwatch
我想在网站上有一个秒表,它在标签上显示运行时间而不重新加载页面。是否可以在客户端执行此操作?我应该使用 Ajax 计时器还是 .net 中的其他东西?
网站使用 C#。
一些链接或演示会很有帮助!谢谢!
【问题讨论】:
标签: ajax asp.net-ajax timer stopwatch
您可以使用 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 将秒数转换为经过时间的函数。
【讨论】:
只能在 javascript 中执行。那里有很多例子。只需google them。这里是one。
【讨论】: