我的按钮点击事件需要很长时间才能处理(> 60分钟)。我试图使用setInterval事件来保持我的会话活着。
function OnButtonClick(s, e, startUrl, progressUrl) {
StartActionOnServer(startUrl);
setInterval(keepalive, 2000);
}
function keepalive()
{
$.ajax({
type: 'POST',
url: '/ImportData/alive',
dataType: "text",
contentType: "text",
success: function (msg) {
console.log(msg);
},
error: function (xhr) {
alert(xhr)
},
});
}
[HttpPost]
public ActionResult alive()
{
var result = "I am pinging the server";
return Content(result);
}
问题在于,我希望setInterval(keepalive, 2000);
在StartActionOnServer(startUrl);
运行约1小时时运行。这是为了让我的视图更新。现在没有发生这种情况。我的Console.log看起来像:
我希望它看起来。
I am pinging the server.
I am pinging the server.
I am pinging the server.
I am done
知道如何实现这一目标。感谢帮助。
更新: 添加了其他使用的方法:
function StartActionOnServer(startUrl) {
positionDate = ReportingPositionDate.GetDate().toDateString();
myProgressBar.Show;
$.ajax({
type: 'POST',
url: startUrl,
data: JSON.stringify({ positionDate: positionDate }),
dataType: "text",
contentType: "application/json; charset=utf-8",
beforeSend: function () { lpImport.Show(); },
success: function (msg) {
console.log(msg);
ImportDataGridView.PerformCallback();
ImportSuccessMessage.SetVisible(true);
ImportSuccessMessage.SetText(msg);
lpImport.Hide();
},
error: function (xhr) {
alert(xhr);
}
});
}
按钮点击事件:
settings.ClientSideEvents.Click = string.Format("function(s, e) {{ OnButtonClick(s, e, '{0}', '{1}'); }}", Url.Action("Start", "ImportData", null), Url.Action("alive", "ImportData", null));
Controller Star动作如下所示:
[HttpPost]
public ActionResult Start()
{
//System.Threading.Thread.Sleep(6000);
//Thread.Sleep(3600000);
Thread.Sleep(10000);
//var result = new { StartResult = "DataFileUpload failed" };
var ReturnVal = "I am done";
progress = 0;
return Content(ReturnVal);
//return Json(result);
}