In my web application I'd like to be able to start a process with certain parameters on the web server, and continuously display output from the process in a text area until it completes. How can I accomplish this with Javascript? I'm using jQuery along with ASP.NET MVC 3.
You can do this with 2 action methods and a javascript timer
[HttpPost]
public JsonResult StartProcess()
{
StartTheMachine();
return new JsonResult() { Data = "Started" };
}
[HttpGet]
public JsonResult GetProcessUpdate()
{
return new JsonResult()
{
Data = GetUpdate(),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
and in your view something like this:
$.post("<%=Url.Action("StartProcess") %>", function(data) {
// do something with data "Started" and start timer
setTimeout(GetUpdate, 5000);
});
function GetUpdate()
{
$.get("<%=Url.Action("GetUpdate") %>", function(data) {
if (data.Complete) // or some way to tell it has finished
{
// do something with other data returned
}
else
{
// call again if not finished
setTimeout(GetUpdate, 5000);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With