0

I have been looking into scopes, global var and local var and tried my best to make it work without success. I hope someone can solve this for meg and maybe I'll finally get how it works.

I have a function to return the status of a IP camera.

console.log(info.IsRecording);

Does print the correct boolean value that I want the function to return. But camInfo holds nothing. So then obviously return camInfo returns undefined.

Function:

function camStatus(camId){
    var camInfo;
    var request2 = new XMLHttpRequest();
    request2.open('GET', 'http://localhost:8124/Json/GetCamStatus?sourceId=' + camId + '&authToken=xxx', true);
    request2.send(null)
    // state changes
    request2.onreadystatechange = function () {
        if (request2.readyState === 4) { // done
            if (request2.status === 200) { // complete   
                var info = JSON.parse(request2.responseText);
                camInfo = info.IsRecording;
                console.log(info.IsRecording);
            }
        }
    }
    return camInfo;
}
Rune Aspvik
  • 145
  • 3
  • 13

0 Answers0