每隔几秒更新一次 MVC 2 视图

发布于 2024-12-09 15:13:20 字数 1584 浏览 1 评论 0原文

我正在使用 MVC 2,我有一个视图,它只显示带有当前时间的标签。

我想每 5 秒更新一次这个视图(标签),这样时间就会更新。我正在使用下面(取自此处),但是似乎不起作用。

public ActionResult Time()
    {
        var waitHandle = new AutoResetEvent(false);
        ThreadPool.RegisterWaitForSingleObject(
            waitHandle,
            // Method to execute
            (state, timeout) =>
            {
                // TODO: implement the functionality you want to be executed
                // on every 5 seconds here
                // Important Remark: This method runs on a worker thread drawn 
                // from the thread pool which is also used to service requests
                // so make sure that this method returns as fast as possible or
                // you will be jeopardizing worker threads which could be catastrophic 
                // in a web application. Make sure you don't sleep here and if you were
                // to perform some I/O intensive operation make sure you use asynchronous
                // API and IO completion ports for increased scalability
                ViewData["Time"] = "Current time is: " + DateTime.Now.ToLongTimeString();
            },
            // optional state object to pass to the method
            null,
            // Execute the method after 5 seconds
            TimeSpan.FromSeconds(5),
            // Set this to false to execute it repeatedly every 5 seconds
            false
        );

        return View();
    }

提前感谢您的帮助!

I am using MVC 2, I have view which simply displays label with current time on it.

I want to update this View(label) every 5 seconds so time will update. I am using below (taken from here) but doesn't seem to be working.

public ActionResult Time()
    {
        var waitHandle = new AutoResetEvent(false);
        ThreadPool.RegisterWaitForSingleObject(
            waitHandle,
            // Method to execute
            (state, timeout) =>
            {
                // TODO: implement the functionality you want to be executed
                // on every 5 seconds here
                // Important Remark: This method runs on a worker thread drawn 
                // from the thread pool which is also used to service requests
                // so make sure that this method returns as fast as possible or
                // you will be jeopardizing worker threads which could be catastrophic 
                // in a web application. Make sure you don't sleep here and if you were
                // to perform some I/O intensive operation make sure you use asynchronous
                // API and IO completion ports for increased scalability
                ViewData["Time"] = "Current time is: " + DateTime.Now.ToLongTimeString();
            },
            // optional state object to pass to the method
            null,
            // Execute the method after 5 seconds
            TimeSpan.FromSeconds(5),
            // Set this to false to execute it repeatedly every 5 seconds
            false
        );

        return View();
    }

Thanks for help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2024-12-16 15:13:20

您所做的将不起作用,因为一旦将初始响应发送到客户端,客户端将不再侦听来自服务器的该请求的数据。您想要做的是让客户端每 5 秒发起一个新请求,然后简单地返回每个请求的数据。一种方法是使用刷新标头。

public ActionResult Time()
{
    this.HttpContext.Response.AddHeader( "refresh", "5; url=" + Url.Action("time") );

    return View();
}

What you are doing won't work as once the initial response is sent to the client, the client will no longer be listening for data from your server for that request. What you want to do is have the client initiate a new request every 5 seconds, then simply return the data for each request. One way to do this is with a refresh header.

public ActionResult Time()
{
    this.HttpContext.Response.AddHeader( "refresh", "5; url=" + Url.Action("time") );

    return View();
}
jJeQQOZ5 2024-12-16 15:13:20

您需要将重复循环放在客户端,以便它每五秒重新加载一次页面。

一种方法是使用 Javascript:

<script>setTimeout("window.location.reload();",5000);</script>

You need to put your recurring loop on the client side so that it reloads the page every five seconds.

One way, using Javascript:

<script>setTimeout("window.location.reload();",5000);</script>
假扮的天使 2024-12-16 15:13:20

您提供的代码在服务器上运行,当页面(在本例中为视图)发送到客户端时,服务器会忘记它!您应该创建一个客户端代码,用于每 5 秒刷新一次页面。您可以使用 header 命令(refresh)或脚本:

<script>
    setTimeout("window.location.reload();", /* time you want to refresh in milliseconds */ 5000);
</script>

但如果您只是想刷新页面以更新Time,我永远不会建议您彻底刷新页面。相反,您可以创建一个 JavaScript 函数,每 5 秒计时一次,并计算当前时间并更新标签。

The code you provided, runs at server, and when a page (View in this case) is sent to the client, the server will forgot it! You should create a client side code for refreshing the page every 5 seconds. You can use a header command (refresh) or a script:

<script>
    setTimeout("window.location.reload();", /* time you want to refresh in milliseconds */ 5000);
</script>

But if you just want to refresh the page for updating the Time, I never suggest you to refresh page completely. Instead, you can create a javascript function, to tick every 5 seconds, and calculate the current time and update the label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文