定义公共方法,方便每个Vue组件调用
ts+eslint
参数说明
fn执行请求的接口
i,定时器标识唯一值,方便多个时统一关闭
time:时间(秒)
params: 接口参数值,可不传
function chartDataReload(fn:any, i:number, time:number,params:any) {
// @ts-ignore
const _this = this
const _loop = async () => {
let data = await fn(params)
_this.chartList[i].clear();
_this.chartList[i].setOption(data,true);
}
_loop();
if(_this.timerList[i]) clearInterval(_this.timerList[i])
_this.timerList[i] = setInterval(_loop, time * 1000);
}
案例:
注意调用call传入this,指向当前作用域
chartReload.call(this, this.initData, 2, 11)
async initData() {
let res = await ...
//处理数据,得到完成option
return options
}