0

The function down here get's called every second, it only never outputs: console.log("noise: "+noise);

So something goes wrong and i have no idea what or how to figure it out.

function readEye() {
    console.log("readEye");

    $.getJSON('output.json', function(data){
        faceDetected = data.faceDetected;
        frameCount = data.frameCount;
        noise = data.noise;
        console.log("noise: "+noise);
    });

    // show values in devPanel
    document.getElementById('faceDetected').innerHTML = "faceDetected: "+faceDetected;

}
4
  • i think you should wait each time until $.getJSON complete loading data Commented Dec 31, 2011 at 12:23
  • What happens if you try to console.log(data); before faceDetected...? Commented Dec 31, 2011 at 12:24
  • if you load it in chrome, hit ctrl+shift+j to open the developer console and set a break point in the script tab on faceDtetcted = data.faceDtected, does it ever reach that point? Commented Dec 31, 2011 at 12:27
  • @Ayman Safadi, - it never shows a log. Commented Dec 31, 2011 at 16:50

2 Answers 2

5

You stumbled upon the "asynchronous trap". $.getJSON() does NOT stop the normal program flow and will NOT wait until finished. So the following line document.getElementById('faceDetected').innerHTML = "faceDetected: "+faceDetected; will be called IMMEDIATELY with an undefined "faceDetected" variable. And this will happen every second, no matter if any of the previous requests ever finishes ....

Generally speaking you should only start a new timeout from inside the callback of an AJAX request. Additionally you should only work with those variables like "faceDetected" or "noise" inside the callback. So if you want to assign those values to a DOM element you should do it INSIDE the callback.

Sign up to request clarification or add additional context in comments.

2 Comments

in other words, everything should be handled within the callback. api.jquery.com/jQuery.getJSON review the success callback exampled in the link
But then why does it not atleast shows "noise: " in the console? Also i looked at the examples but i can't even get a callBack to work. I will make a new post since there is no space here.
1

Also check for format of output.json - it must be well-formed and pass $.parseJSON function (http://api.jquery.com/jQuery.parseJSON/). As stated in documentation (http://api.jquery.com/jquery.getjson/):

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

AND

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason.

3 Comments

my json looks like this: { "noise": 0.21046373, "framecount": 259, "faceDetected": true }
but then with returns but they never work for me in comments.
your json is {"noise": 0.21046373, "framecount": 259, "faceDetected": true} but you call for data.frameCount. What if you change to data.framecount? I think this might cause silent fall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.