java轮询请求接⼝_js调⽤轮询接⼝
> 项⽬中遇到需要很多个需要轮询处理的接⼝,然后简单的封装了下,做个记录,以后⽤到类似的直接copy > // polling-utils.jsjava调用js的ajax
/**
* @descripting 轮询功能
* @param {String} type 请求类型
* @param {String} url 地址
* @param {Object} data 请求数据
* @param {Number} delay 轮询间隔时间
*/
export default function polling(type, url, data, delay = 1000) {
return new Promise((resolve, reject) =>{
ajax[type](url, data).then(res => {
if (res.data === 'polling') { // 这个继续进⾏轮询的条件,需要根据⾃⼰的需要修改
setTimeout(() => {
resolve(polling(type, url, data, delay));
}, delay)
} else {
resolve(res);
}
})
})
}
#### ⽤法 ####
import polling from 'utils/polling-utils';
polling('post', url, data).then(res => { console.log(res) })

发表评论