我想在网站上有一个秒表,它在标签上显示运行时间而不重新加载页面。是否可以在客户端执行此操作?我应该使用 Ajax 计时器还是 .net 中的其他东西?
网站是用 C# 编写的。
一些链接或演示会非常有帮助!谢谢 !
我想在网站上有一个秒表,它在标签上显示运行时间而不重新加载页面。是否可以在客户端执行此操作?我应该使用 Ajax 计时器还是 .net 中的其他东西?
网站是用 C# 编写的。
一些链接或演示会非常有帮助!谢谢 !
您可以使用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 秒”。这是一个快速的 JavaScript函数,可以将秒数转换为经过的时间。