vue定时发送请求
我这个可能比较麻烦,初始时msg
没有值,在amounted()
中调用一次,之后监听msg
值的变化,如果有变换,则延时1秒再请求数据。由此实现请求的更新。
data(){
return{
msg:""
}
},
watch:{
msg:function(){
clearTimeout(this.timer); //清除延迟执行
this.timer = setTimeout(()=>{ //设置延迟执行
axios
.get('http://127.0.0.1:8080/api')
.then(response => (this.msg = response.data.getter))
.catch(error => console.log(error))
},1000);
}
},
mounted () {
axios
.get('http://127.0.0.1:8080/api')
.then(response => (this.msg = response.data.getter))
.catch(error => console.log(error))
}