我应该使用Ajax的计时器? [英] Should I use Ajax Timer?

查看:222
本文介绍了我应该使用Ajax的计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有的网站上,它显示在标签上运行时间没有重新加载页面秒表。是否有可能做到这一点在客户端?我应该使用Ajax计时器或其他水木清华从.NET?

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 from .net?

网站是在C#。

某些链接或演示将是非常有益的!谢谢!

Some links or demos would be really helpful ! Thanks !

推荐答案

您可以用基本的JavaScript使用的的setTimeout

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();

更新:如果用户将是你可能想显示一个稍微人性化的消息,然后同时在页面上你已经花了1000秒此页。 这里有一个快速的JavaScript 功能会秒转换成时间已过。

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.

这篇关于我应该使用Ajax的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆