In the code below you can see async functions, first one to fetch some data, and the second is to build a graph with data that have just been fetched. It works perfectly but I would like to refresh the data each second. I have put setInterval everywhere but nothing works...
Here is the code:
chartSet();
const xAxis = [];
const yAxis = [];
async function resetChart() {
const datas = await fetch(
"https://canvasjs.com/services/data/datapoints.php"
);
const datasJson = await datas.json();
datasJson.forEach((elt) => {
xAxis.push(elt[0]);
yAxis.push(elt[1]);
});
}
async function chartSet() {
await resetChart().catch(() => {
console.warn("Didn't work");
});
const ctx = document.getElementById("myChart3").getContext("2d");
const myChart = new Chart(ctx, {
type: "line",
data: {
labels: xAxis,
datasets: [
{
label: "Random datas",
data: yAxis,
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 1,
},
],
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js" integrity="sha512-d9xgZrVZpmmQlfonhQUvTR7lMPtO7NkZMkA0ABN3PHCbKA5nqylQ/yWlFAyY6hYgdF1Qh6nYiuADWwKB4C2WSw==" crossorigin="anonymous"></script>
<canvas id="myChart3"></canvas>
Thank you for your help!