I am struggling to store accumulated data between ajax calls in an object. Even though I can retrieve the data from each call, I cannot seem to merge it into a single array.
To address this issue, I have devised the dataAccumulator()
function. The intention is that by invoking accumData.append
, the data from a solitary call will be added to the holding array.
Despite my efforts, I continuously encounter errors such as
Cannot read property 'append' of undefined
or Uncaught TypeError: dataAccumulator is not a function
, regardless of where and how I define or employ the function...
Consider the following code snippet:
var inter;
var dataAccumulator = dataAccumulator(); //object of interest
function startTempMonit()
{
$(document).ready(function()
{
time= 0;
inter = setInterval(function()
{
$.ajax // ajax call starts
({
//not relevant for the question. arguments removed...
})
.done(function(data) {
//problem here. data is foreach call.
//I would like to accumulate each of the data to handle the whole here.
dataAccumulator.append(data);
});
time= time + 0.5;
}, 500)
});
};
function dataAccumulator () {
let accumData = [];
console.log("accumData function created")
function append(data) {
accumData.push(data);
console.log(accumData); //log the accumulated data each time
}
this.append = append;
};
It appears that my predicament revolves around JavaScript scopes. My objective is to maintain the accumData
array accessible within the ajax call's .done
function. This encapsulates the crux of my dilemma.