views:

302

answers:

1

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?

+1  A: 

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

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