I am trying to make progress bar. On the server side I created a controller, which should get the progress value and store the value in session.
On the client side I created two ajax requests. One of them starts a function, which I want to be monitoring, and the other one just checks the progress stage. I mean, that's how I thought it would work. But it only outputs something when it's done.
It just waits for few seconds and then alerts "Done!" and that's all.
What's the problem? Maybe I should to create new thread in the controller to monitoring the progress stage?
Here's the client side code:
function generate() {
setTimeout(checkProgress, 400);
$.ajax({
type: "POST",
url: "/PPTReport/Generate",
async: true,
success: function (text) {
console.log(text);
},
error:function() {
console.log("error! error!");
}
});
}
function checkProgress() {
var id = "reportProgress";
$.ajax({
type: "POST",
url: "/PPTReport/GetProgress",
async: true,
data: "id="+id,
success: function (data) {
if (data.done) {
console.log('Done!');
} else {
console.log('Progress at ' + data.percent + '%');
console.log(data.percent);
setTimeout(checkProgress, 100 );
}
},
error: function() {
console.log('ajax error');
}
});
}
Here's the server side code
public class ProgressInfo
{
public int Percent { get; set; }
public bool Done { get; set; }
}
public JsonResult GetProgress(string id)
{
ProgressInfo progress;
if (string.IsNullOrEmpty(id)
|| (progress = Session[id] as ProgressInfo) == null)
{
return Json(new
{
success = false
});
}
if (progress.Done)
{
Session.Remove(id);
}
return Json(new
{
success = true,
done = progress.Done,
percent = progress.Percent
}
);
}
public JsonResult Generate(){
//init stuff
//there are somtheing like that
ProgressInfo progress = new ProgressInfo();
progress.Percent = 0;
progress.Done = false;
if (tabs > 0)
{
FirstPage();
progress.Percent++;
Session["reportProgress"] = progress;
if (tabs > 1)
{
SecondPage();
progress.Percent++;
Session["reportProgress"] = progress;
if (tabs > 2)
{
ThirdPage();
progress.Percent++;
Session["reportProgress"] = progress;
if (tabs > 3)
{
LastPage();
}
}
}
}
//what we've gonna return stuff etc
}
UPD: well, I finally kinda make it - I made test function (almost like in example) Here's the code:
js:
function doCalculation() {
$.post('/PPTReport/DoCalculation/sas');
setTimeout(pollProgress, 1000);
}
function pollProgress() {
var progressID = "sas";
$.post('/PPTReport/GetProgress/' + progressID, function (response) {
if (!response.success) {
alert('Cannot find progress');
return;
}
if (response.done) {
alert('Done!');
} else {
alert('Progress at ' + response.percent + '%');
setTimeout(pollProgress, 1000);
}
}, 'json');
}
server-side:
public void DoCalculation(string id)
{
ProgressInfo progress = new ProgressInfo();
if (!string.IsNullOrEmpty(id))
{
Session[id] = progress;
}
//periodicly update progress
int i = 0;
while(i == 0 && progress.Percent != 7600000340)
{
progress.Percent++;
Thread.Sleep(1000);
}
}
public JsonResult GetProgress(string id)
{
ProgressInfo progress;
if (string.IsNullOrEmpty(id)
|| (progress = Session[id] as ProgressInfo) == null)
{
return Json(new
{
success = false
});
}
if (progress.Done)
{
Session.Remove(id);
}
return Json(new
{
success = true,
done = progress.Done,
percent = progress.Percent
});
}
but I wait more than a minute until the action will execute for the first time!
Look
Why does it happen? It just calling simple function, why does it wait so long?