function fetchData() {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:8080/data/getMaxList", false);
xhr.onload = function () {
if (xhr.status === 200) {
var responseData = xhr.responseText;
responseData = JSON.parse(responseData);
var maxArray = Object.values(responseData[0]);
resolve(maxArray);
} else {reject(new Error("请求失败"));}
};
xhr.send();
});
}
setInterval(function(){
fetchData().then(function (maxArray) {
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: maxArray,
type: 'bar'
}
]
};
option && myChart.setOption(option);
});
}, 1000);
Ajax请求与echarts.js结合promise异步处理
最新推荐文章于 2024-08-19 10:59:54 发布