0

i've a strange problem with JS (probably a noob bug), but i'm stuck with it

In function fillInVersionsList, if i put an alert("tempo") or a break in firebug, i can access to my datas in parameter (ie : alert(pSimulator.simulatorData['LastVersion']) and i've the right result. The problem is that if i don't put an alert/firebug break before my access to datas, i've a JS error pSimulator.simulatorData is undefined.

$(document).ready(function() {

    var simulator = new Simulator();

    // Load SimulatorData into the simulator class
    initSimulatorData(simulator);
    // Fill in datas into VersionsList (2nd arg = Id of the list)
    fillInVersionsList(simulator, $('#VersionsList'));  


});

function initSimulatorData(pSimulator)
{
    $.ajax({
      url: "getData.php?action=init",
      success: function(data) {
          pSimulator.initSimulatorData(data);
        }

    });

}


function fillInVersionsList(pSimulator, pSelect)
{
    //alert("tempo");
    alert(pSimulator.simulatorData['LastVersion']);

    pSelect.html('<option>test</option>')       

}

function Simulator()
{

    var simulatorData;


    this.initSimulatorData = function(pSimulatorData)
    {
        this.simulatorData = pSimulatorData;    
    }




}

Is there something to solve this problem?

Thanks in advance

1
  • as I can see, the function is called, but what does pSimulator contain? it could be that there is no simulatorData in pSimulator Commented May 11, 2012 at 9:57

2 Answers 2

1

I suspect initSimulatorData is loading some data asynchronously.

Adding the alert gives it long enough for the data to be loaded.

You will need to add some sort of callback function, eg:

initSimulatorData(simulator, function () {
    // Fill in datas into VersionsList (2nd arg = Id of the list)
    fillInVersionsList(simulator, $('#VersionsList'));  
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, think this is the problem, but the success: parameter of $.ajax isn't supposed to be a callback? (i've added the missing functions, sorry)
0

Whats looks like from your problem is that simulator is taking time to initialize and when fillInVersionsList is called pSimulator is still not completely initalized.

When you put an alert it is getting some time delay by which time simulator is initalized.

Check if there is any callback method after simulator is completely initialized and then call fillInVersionsList method after that.

what does initSimulatorData(simulator) does? Is there any asynchronous code invloved in this?

2 Comments

You have either to make the ajax call synchronous or call the fillInVersionsList from the sucess: funtion(){pSimulator.initSimulatorData(data); // here call fillInVersionsList() }
I've added "async : false" parameter in my ajax request, and it works now, Thanks you :)

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.