How to use a function to create multiple ajax refreshable charts with chartjs?

I use this function to avoid repeating the code to create 4 different charts:

function renderDoughnutChart(elementId, labels, data, bgColor, borderColor) {

    var ctx = document.getElementById(elementId);

    var myChart = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: labels,
            datasets: [{
                data: data,
                backgroundColor: bgColor,
                borderColor: borderColor,
                borderWidth: 1
            }]
        },
        options: {
          animation:{
              animationScale:true
          }
        }
    });

}

these charts have to be changed based on the ajax response. it works in first time rendering but when ajax response refreshes this error apears and nothing happens.

Canvas is already in use. Chart with ID ‘0’ must be destroyed before
the canvas can be reused.