views:

853

answers:

1

I am executing a function where first I am making cursor to wait state(hourglass) and then I am sending a synchrounous AJAX request .After getting the response I am making cursor to default state.

The Actual Code is this..

// tests the smtp settings function TestSettings() { var buttonparams= new Object();

buttonparams.IsCommandButton = true;
buttonparams.ButtonId = "testsettings";
buttonparams.ButtonText = "Sending Test Mail...";
buttonparams.ButtonOrigText = "Test Settings";

if(buttonparams.IsCommandButton == true)
 HandleButtonStatus(true, buttonparams);

var request = function()
{
 var ret = SendForm(buttonparams);

 alert(ret);

}
window.setTimeout(request, 0);

}

function SendForm(pButtonParams) { var http; var formdata;

http = yXMLHttpRequest();

http.open("POST", "./", false);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Req-Type", "ajax");
formdata = xEncodePair("_object", "PrefMgr")+ "&";
formdata += xEncodePair("_action", "SmtpTest")+ "&";
formdata += GetEncodedFormData(); 

http.send(formdata);

if(http.status == 200)
{ 
 if(pButtonParams.IsCommandButton == true)
  HandleButtonStatus(false, pButtonParams);

 return (http.responseText);
} 
else
{

 return ("Error " + http.status + ": " + http.statusText); 
}

}

function HandleButtonStatus(pIsButtonStatusChange, pButtonParams) { var button = yById(pButtonParams.ButtonId);

if(pIsButtonStatusChange)
{
        document.body.style.cursor = "wait";
 button.value = pButtonParams.ButtonText;
 button.disabled = true;

}
else
{
 document.body.style.cursor = "default";
 button.disabled = false;
 button.value = pButtonParams.ButtonOrigText;
}

}

+1  A: 

Try to assign:

var st = document.body.style;

and then refer to st in both functions. This could be a scope issue in AJAX callback function.

EDIT: Use callback function to restore cursor shape. Don't forget to do the same in case AJAX call fails.

Thevs
ya..now it is working fine..but one more issue..I am creating an object before making cursor wait for my application purpose. If I am creating it, then again HourGlass is not working in google chrome. It I am commenting the create object statement then hour glass is working. why?I will update the given code.
Ratna Kumar
I think you should provide the full code or this function. There is something related to other things.
Thevs
Now I got it. The request function is a classic closure. When you create var obj, you create a global context which keeps `request` function pointer from freeing.Here is how it works: When `request' function is called first - it works as expected, sending AJAX request. On AJAX completion the context of `request` function is no more existing, unless you keep it by defining `obj` in outer scope, thus creating a closure.Even synchronous AJAX calls works asynchronously via callback functions. You should better set cursor in callback function (using onstatechange) and not in `request`.
Thevs
sorry..I might be confusing you..I am posting the actual code I have written
Ratna Kumar
I think that Google Chrome does all XHR asynchronously (i.e. in separate thread), even it set to synchronous. Try to move all your actions to `onreadystatechange` callback function.
Thevs