Why reusing emptied array in Javascript not possible? -
this question has answer here:
i programmed in few programming languages, time time have experience javescript.
here situation: have array global variable (myarray). need refill myarray data db when event happens. wrote myfunction, in myarray emptied remove eventual older data, filled again new data.
if don't delete array content, works, (of course) array may contain older data , grow each time function called, , don't want that.
what don't understand following: if delete content of myarray when myfunction starts, i cannot refill again...
here simplified version of code, help:
myarray = []; // global variable // in myfunction //first empty array, refill data db { // let's empty myarray, tried 4 different ways -- 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 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 delete content of myarray, print nothing; //else if don't delete content, array contains new , old data for(ix in myarray){ console.log("in myarray got " + myarray[ix]); } } //myfunction end
--edits--
i see question marked duplicated, maybe is, couldn't find question suited situation.
i see answers listed maybe problem in synchronization, have learn topic
Comments
Post a Comment