0

I'm running into an issue with an async call to the server that only works one time, then it appears to become a synchronous call. Let me try to explain.

It's an MVC 2.0 site, using ASP.NET and Ajax. I'm using the Ajax.BeginForm helper, like so:

<% using (Ajax.BeginForm("Start", null,
new { virtualMachineId = xyz },
new AjaxOptions { UpdateTargetId = "VirtualMachineForm", OnBegin="OnStartingVm" }
)){

Then while the machine is starting I want to call back to the server and get an update every second. It works the first time correctly, then changes behavior. OnStartingVm looks something like this:

function OnStartingVm() {
    $('#StartingDiv').css('visibility', 'visible');
    $('#StartingDiv').show();
    var vmId = xyz;

    intervalId = setInterval(function () {
        updateStartingStatus(vmId)
    }, 1000);
}
function updateStartingStatus(vmId) {
    /* This part always runs */
    $.ajax({
        url: "/member/vm/getstartingstatus/" + vmId,
        dataType: 'json',
        async: true,
        success: function (data) {
          alert('This part runs every second on the first time only');
           if (data.status == "Running") {
              $('#StartingDiv').text(data.percentComplete);
           }
           else {
              $('#StartingDiv').css('visibility', 'hidden');
              $('#StartingDiv').hide();
              clearInterval(intervalId);
           }
        },
    });
}

Within the updateStartingStatus function, the first part runs every second, every time. However, within the Ajax call, the success result works every second on the first time only. Then on the second time I click on the start button all of the requests queue up. After the starting has completed, about 20 seconds later, I get a bunch of alert windows back to back. So, I can tell that updateStartingStatus runs every second every time, but the ajax call appears to switch to become a sync call after the first time.

Refreshing the browser window doesn't help. I have to fully close it and open it again. The same occurs in IE and Chrome.

One more thing to note is that the updated div (VirtualMachineForm) contains most of the page, including the button being pressed. So it basically replaces the page from under itself. Not sure if that would cause any issues.

Additionally, if I debug in Visual Studio 2010, the call isn't made to the controller action when the issue occurs. So, it appears to be something client-side. I've ruled out any issues server-side.

tereško
  • 58,060
  • 25
  • 98
  • 150
Scott Forsyth
  • 1,194
  • 1
  • 7
  • 15

2 Answers2

1

I eventually figured it out. This post lead to the answer.

It was session state related and the browser locked the request until a previous one was completed. I didn't need to disable session state, but I had to avoid a session write from code.

That explains why a browser refresh didn't work and why I had to close and open the browser again.

Community
  • 1
  • 1
Scott Forsyth
  • 1,194
  • 1
  • 7
  • 15
0

Why don't you call clearInterval function?