Friday, April 8, 2011

Show process status using AJAX and ASP.NET MVC

I have a method in my controller that kicks of a job on the server. I'll use a Ajax call (jQuery) to start this via a link on the page. This can take a while and I need to show some status on the page. I don't necessarily need "26% done ..." but more a status showing "working" or "done".

How do a get jQuery to continuously pull for the status of the job? Or should it be solved differently?

From stackoverflow
  • You will need some sort of state to hold the status of the task which both the task and Javascript can Access. For example Server Cache. Say you use a key of TaskComplete.

    Kick off your task with javascript through AJAX

    While Your Task Is Running

    Task updates Server Cache Item

    End While

    Parallel

    Use Javascript to poll the cache for a value

    Kick off your task with javascript through AJAX

    While Ajax Response Not Task Complete

    wait 2 seconds

    Call a javascript function through ajax again to read the value of the Server Cache Item

    Execute relevant task based on its value

    An Article you may find useful is here:

    http://msdn.microsoft.com/en-us/magazine/cc163393.aspx

    Riri : OK. Good that's what i thought. But how do I do this "loop"/"timer" in jQuery to keep pulling for status?
    kgiannakakis : Use: setTimeout(your_callback, timeout_in_ms);
    REA_ANDREW : Yes, using the setTimeout though I find it better to use the following syntax var t = setInterval(function(){//execute your function here},milliseconds); When finished clearInterval(t);
    REA_ANDREW : Or find a Jquery Timer Plugin. Both would be sufficient. Hope this helps :-)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.