简体   繁体   中英

Update MVC 2 view every few seconds

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!

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

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM