Reputation: 403
Will xmlHttp get garbage collected and if so, when ?
function foo (param)
{
var xmlHttp = GetXmlHttpRequestObject();
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
// do something involving param and xmlHttp.responseXML
}
}
xmlHttp.open("GET", "GetAsyncData.ashx", true);
xmlHttp.send();
}
Upvotes: 0
Views: 159
Reputation: 123397
Yes, the garbage collector will destroy automatically a variable as soon as the function foo
has been executed (since the variable is local and thus it cannot be used outside the given scope).
This is also one of the reasons why is a good practice to especially use local variables declared with var
keyword.
Please note that in your specific context, since you perform an asynchronous ajax call, your function is returning while the ajax call is still running so the garbage collector will destroy your xmlHttp
variable when the both the function is returned and the call has been completed
Upvotes: 1