vue.js计时器定时触发接口,展示消息提示框
时间: 2024-05-07 21:21:29 浏览: 210
你可以使用Vue.js中的定时器setInterval()方法来实现定时触发接口,然后根据接口返回的消息展示相应的提示框。
以下是一个简单的示例:
```
<template>
<div>
<button @click="startTimer">开始计时</button>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: '',
timer: null
}
},
methods: {
startTimer() {
this.timer = setInterval(() => {
// 调用接口获取消息
axios.get('/api/getMessage')
.then(response => {
this.message = response.data.message
// 展示消息提示框
this.$message({
message: this.message,
type: 'success'
})
})
.catch(error => {
console.log(error)
})
}, 5000) // 每5秒触发一次接口
}
},
beforeDestroy() {
clearInterval(this.timer)
}
}
</script>
```
在上面的示例中,我们使用了Vue.js的计时器setInterval()方法来每隔5秒触发一次接口,获取消息并展示提示框。同时,在组件销毁前我们使用clearInterval()方法清除了计时器,避免内存泄漏问题。提示框的展示我们使用了Element UI组件库中的$message方法。你可以根据自己的需要进行修改和扩展。
阅读全文
相关推荐

















