js調用輪詢接口


項目中遇到需要很多個需要輪詢處理的接口,然后簡單的封裝了下,做個記錄,以后用到類似的直接copy
// polling-utils.js

/**
     * @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) })



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM