最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger an API every 3 seconds in the vue framework? - Stack Overflow

programmeradmin1浏览0评论

I am a vue beginner and would like to ask a question! My English is not good but I try to describe my problem pletely, thank you.

Currently I use vue to learn how to access the API, but I hope to be able to automatically touch the API again every 3 seconds to update the screen, but I really don’t know how to achieve this?

Hope to get your help, thank you again for watching my question.

My example

I am a vue beginner and would like to ask a question! My English is not good but I try to describe my problem pletely, thank you.

Currently I use vue to learn how to access the API, but I hope to be able to automatically touch the API again every 3 seconds to update the screen, but I really don’t know how to achieve this?

Hope to get your help, thank you again for watching my question.

My example

Share Improve this question edited Aug 10, 2021 at 8:45 Boussadjra Brahim 1 asked Aug 9, 2021 at 14:11 WEI AWEI A 4014 silver badges12 bronze badges 1
  • developer.mozilla/en-US/docs/Web/API/… – AdityaParab Commented Aug 9, 2021 at 14:14
Add a ment  | 

2 Answers 2

Reset to default 8

Add setInterval in created hook that calls the method that loads the data :

Vue.createApp({
    data() {
    return {
      status: true,
      data: [],
      interval:null
    };
  },
  methods: {
    reNew() {
      axios.get("https://randomuser.me/api/?results=5").then(
      (response) =>
          // console.log(response)
          (this.data = response.data.results)
      )
    }
  },
  mounted() {
    this.reNew()
  },
  created(){
    this.interval = setInterval(() =>{
      this.reNew()},3000)
  },
  destroyed(){
    clearInterval(this.interval)
  }
}).mount('#app');

You can use the setInterval method.

This would execute the function every 3 seconds.

setInterval(function(){ 
// fetch data...
}, 3000);
发布评论

评论列表(0)

  1. 暂无评论