js 实现简单的轮询

在实际开发中,经常会有轮询的效果。

1、js实现轮询效果==》使用setTimeout,clearTimeout方法

function setTimer () {
         let timer
        axios.post(url, params)
        .then(function (res) {
            if(res){
                console.log(res);
                timer = setTimeout(() => {
                    this.setTimer()
                }, 5000)       
            }else {
                clearTimeout(timer) //清理定时任务
            }
            
        })
        .catch(function (error) {
                console.log(error);
        });
},                    

 2.HTML5推出新的对象:websocket

 

posted @ 2019-08-22 15:32  三高娘子  阅读(24396)  评论(0编辑  收藏  举报