Skip to content Skip to sidebar Skip to footer

Unable To Alter Json Variable Within A Function

Altering a JSON variable is failing for following snippet: var data = {status: ''}; rosconnection.setOnOpen(function (e) { data.status = 'Succeeded'; alert('succes

Solution 1:

You didn't show us how you know the status didn't change so...

My bet is: there is no way you saw the alert without the data being changed so your code probably looks something like this:

var data = {status: ''};

rosconnection.setOnOpen(function(e) {
        data.status = 'Succeeded';
        alert('success');
});

rosconnection.setOnError(function(e) {
        data.status = 'Failed';
        alert('fail');
});

alert(data.status);

So the status was not set yet. Check it inside the callback. AJAX... What does AJAX means? A is for async, which means it will fire sometime in the future(near or far), you can't know when and sometimes don't even if it will ever be called.

Updated version:

var data = {status: ''};

rosconnection.setOnOpen(function(e) {
        data.status = 'Succeeded';
        alert(data.status);
});

rosconnection.setOnError(function(e) {
        data.status = 'Failed';
        alert(data.status);
});

Post a Comment for "Unable To Alter Json Variable Within A Function"