Should I use Ajax Timer?

谁都会走 提交于 2019-12-10 12:29:27

问题


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?

Website is in C#.

Some links or demos would be really helpful ! Thanks !


回答1:


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.




回答2:


It is possible to do in javascript only. There are so many example out there. Just google them. Here's one.



来源:https://stackoverflow.com/questions/2336958/should-i-use-ajax-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!