title: call method every so often vue js
body: <p>I need to call a function every so often, what I want is to update the data every so often for this reason I need to call that method, I'm using vue js.</p>
  
  <p>in the same way I want to know in which property of vue js locate it, thanks, I will investigate all the comments</p>
  
  <p>I need to call the 'listar()' method every 30 seconds</p>
  
  <pre><code>&lt;script&gt;
    export default {
      methods: {
  
              listar(){
              let me=this;
                  me.mesas=[];
                  axios.get('api/mesas/ListarTodos').then(function(response){
                      //console.log(response);
                      me.mesas=response.data;
                        me.loading=false;
                  }).catch(function(error){
                      console.log(error);
                  });
  
  
  
          },
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>This did not work for me</p>
  
  <pre><code>setTimeout(() =&gt; {
           //
  }, 300)
  </code></pre>
  
  <p><strong>Upgrade 1</strong></p>
  
  <p>this code actually works for me,but the problem is — it will keep running after you switch to another page, because it’s a single page application.</p>
  
  <p>I'm using clearInterval () but it does not work, the method keeps running even though I change the page (component).
  Clear the clear only the first time I enter another page, then no longer</p>
  
  <p>Ref--><a href="https://renatello.com/vue-js-polling-using-setinterval/" rel="nofollow noreferrer">https://renatello.com/vue-js-polling-using-setinterval/</a></p>
  
  <pre><code>&lt;script&gt;
  import axios from 'axios'
    export default {
  
  data () {
        return {
          polling: null,
  
         },
  methods: {
  
          listar(){
                  let me=this;
                      me.mesas=[];
                      axios.get('api/mesas/ListarTodos').then(function(response){
                          //console.log(response);
                          me.mesas=response.data;
                            me.loading=false;
                      }).catch(function(error){
                          console.log(error);
                      });
  
  
  
       pollData () {
        this.polling = setInterval(() =&gt; {
  
           this.listar();
         }, 3000) },
  
                  },
  
  
   created () {
        this.pollData()
  
      },
  
   beforeDestroy () {
           clearInterval(this.polling)
      },
    }
  &lt;/script&gt;
  </code></pre>