我有一个ASP.NET MVC页面,其中有一个名为“Start Live Meeting”的按钮。
当用户点击此按钮时,它会调用一个名为“StartLiveMeeting”的控制器方法,该方法返回一个字符串。
如果控制器返回空字符串,那么我希望Timer调用Controller方法,直到它返回字符串。我正在使用jquery.timer.js插件(http://plugins.jquery.com/files/jquery.timers-1.2.js.txt)
单击按钮,将调用控制器方法。但是Timer没有启动。我指定了5秒来调用控制器方法。
感谢您的回复。
ASPX页面上的代码:
//When "Start Meeting" button is clicked, if it doesn’t return empty string, Display that text and Stop Timer. Else call the Timer in every 5 sec and call the StartLiveMeeting Controller method.
$("#btnStartMeeting").click(function() {
var lM = loadLiveMeeting();
if (lM == "") {
$("#btnStartMeeting").oneTime("5s", function() {
});
} else {
$("#btnStartMeeting").stopTime("hide");
}
return false;
});
function loadLiveMeeting() {
$("#divConnectToLive").load('<%= Url.Action("StartLiveMeeting") %>', {}, function(responseText, status) {
return responseText;
});
}
<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" runat="server">
<div id="divStartMeetingButton"><input id="btnStartMeeting" type="submit" value="Start Meeting" />
</div>
<div id = "divConnectToLive">
<div id="loading" style="visibility:hidden">
<img src="../../img/MedInfo/ajax_Connecting.gif" alt="Loading..." />
</div>
</div>
控制器方法:
[HttpPost]
public string StartLiveMeeting()
{
int intCM_Id = ((CustomerMaster)Session["CurrentUser"]).CM_Id ;
var activeMeetingReq = (from m in miEntity.MeetingRequest
where m.CustomerMaster.CM_Id == intCM_Id
&& m.Active == true
select m);
if (activeMeetingReq.Count() > 0)
{
MeetingRequest meetingReq = activeMeetingReq.First();
return "<a href='" + meetingReq.URI + "'>" + "Connect to Live Meeting</a>";
} else {
return "";
}
}
答案 0 :(得分:1)
load()
方法是异步的,因此您需要使其同步,或将响应逻辑放在回调中。
$("#btnStartMeeting").click(function() {
loadLiveMeeting();
return false;
});
function loadLiveMeeting() {
$("#divConnectToLive").load('<%= Url.Action("StartLiveMeeting") %>', {}, function(responseText, status) {
if (responseText == "") {
$("#btnStartMeeting").oneTime("5s", function() {
// call load meeting again
});
} else {
$("#btnStartMeeting").stopTime("hide");
}
});
}