Odd variable scope issue in .ajax()

Odd variable scope issue in .ajax()

Hi all,

Apologies if this is actually a general javascript issue but I've only come across it is this instance.

I have an asmx web service which contains a few methods that I am calling from .ajax(). For the example, I have one called CountFolders(path), which simply does that, returns an int (as a string .. don't ask) of the number of folders contained in the supplied path. I have tested this method and it works and I have also set a break point in it whilst running the example below and it is returning the correct number.

Basically I need to get a result from this method and then use the vale later on to determine whether other code should execute. The example below is an extract of the code concerned.

var folderCount = 0

$.ajax({
type: "POST",
url: "wf24FileService.asmx/CountFolders",
data: "{ 'path' : '" + path + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(r) {
folderCount = parseInt(r.d);
}
});

if (folderCount > 0) {
// do something here
} else {
// do something else
}

If I break along the way and examine folderCount, it is 0 after declaration, inside the "success" function it is 2 (which is correct), but when I get down the "if" block it is back to zero again.

This looks like some strange scoping issue but I was of the impression that because I have not redeclared the folderCount variable within the "success" function that it should be the same variable.

Any ideas? Any help greatly appreciated as I don't have much hair left to pull out.

Thanks.