I'm trying to assign a value to a global variable, but am encountering a scope problem. For example, I extract the value resp[Q0]
within a function and need to assign this to the variable PID
which is outside the function.
I'm doing this, because I then need to store this value in a dictionary (additionalInfo
) with other variables. Below is my stripped down code, but my script is significantly longer, so it does need to occur in this odd organization.
You'll see that within the function PID
is properly assigned a value. Outside the function, it doesn't. (See my console.logs) So currently I'm receiving an error that PID
is undefined.
var PID;
var collectPID = {
timeline: [text],
loop_function: function(data) {
resp = JSON.parse(data.values()[0]['responses']);
PID = resp['Q0'];
console.log('this works', PID);
}
};
console.log('this doesnt work', PID);
var additionalInfo = {
SONA: PID,
other: info,
on_finish: main_on_finish
}