use a function to store each ajax call data

I want to store data between ajax calls, so that I can have my accumulated data in an object. I can get the data of each of the calls, but I am not able to accumulate the data in one whole array.

I created the dataAccumulator () function, so that when I call accumData.append the data for a single call can be pushed to the holder array.

I keep getting the error Cannot read property 'append' of undefined or Uncaught TypeError: dataAccumulator is not a function no matter where I declare or call the function…

This is the code:

var inter;
var dataAccumulator = dataAccumulator(); //object of interest

function startTempMonit()
        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.
            time= time + 0.5;
        }, 500)

function dataAccumulator () {
    let accumData = [];
    console.log("accumData function created")

    function append(data) {
        console.log(accumData); //log the accumulated data each time

    this.append = append;

I guess my problem is with js scopes. I want to keep my accumData array available inside the ajax call .done. That is the summary of my problem.


The first step is always to get rid of global variables. The second step is to use events instead of global state.

Register a callback that is called every time data has arrived. Then work with the data in the callback.

function startTempMonit(callback) {
    var time = 0;
    return setInterval(function () {
            url: '....',
            data: '.....' + tiempo
            dataType: '.....',
        .done(function (data) {
            console.log("received", data);
        time += 0.5;
    }, 500);

$(function () {
    var inter, accumData = [];
    $("#buttonStart").click(function () {
        inter = startTempMonit(function (data) {
            // do something, i.e. add it to a table or a chart
    $("#buttonStop").click(function () {