I programmed in a few programming languages, from time to time i have some experience also with Javescript.
Here my situation: I have an array as global variable (myArray). I need to refill myArray with data from the DB when some event happens. So I wrote myFunction, in which myArray is emptied to remove eventual older data, then filled again with the new data.
If I don't delete the array content, everything works, but (of course) the array may contain also older data and will grow each time the function is called, and I don't want that.
What I don't understand is the following: If I delete the content of myArray when myFunction starts, I cannot refill it again...
Here the simplified version of my code, thanks everyone for any help:
myArray = []; // global variable
// IN MYFUNCTION
//first empty the array, then refill it with data from DB
{
// Let's empty myArray, tried all 4 different ways to do that -- no difference:
// Way 1)
//while(myArray.length > 0) { myArray.pop(); }
//OR Way 2)
//myArray.length=0;
//OR Way 3)
//myArray.splice(0,myArray.length);
//OR Way 4)
myArray = [];
dataNamesQuery = "SELECT something FROM db according user's input";
DatabaseInterface.get_data_now(dataNamesQuery).get({}, function(result){
allDataNames = result.Results;
for(i in allDataNames){
myArray.push(allDataNames[i].something);
}
});
}
//IF I DELETE THE CONTENT OF MYARRAY, THIS WILL PRINT NOTHING;
//ELSE IF I DON'T DELETE ITS CONTENT, THE ARRAY CONTAINS NEW AND OLD DATA
for(ix in myArray){
console.log("in myArray got " + myArray[ix]);
}
} //MYFUNCTION END
--edits--
I see that this question is marked as duplicated, maybe it is, just I couldn't find a question that suited to this situation.
I see from answers you listed that maybe the problem is in synchronization, so I have to learn about this topic