CodeNA
CodeNA

Reputation: 3

Vue.js - Why does the function repeats every time?

Hi I am trying to execute this code with vuejs just every time that I go into the

component:

created() {
     this.getPosts();
     setInterval(function () {
        this.getPosts();
     }.bind(this), 2000);
},

I want to execute the serInterval just one time, just when I go into the component how I said before but it keeps reloading every single moment, I do not want that, how can I do that this serInterval execute only one?

Upvotes: 0

Views: 277

Answers (2)

Younes
Younes

Reputation: 421

Because that's what setInterval is supposed to do, if you are willing to stop it manually think about adding clearInterval() to your code, if not, setTimeout might do your job.

Check Documentation

Upvotes: 0

user14340642
user14340642

Reputation:

You should use setTimeout instead :

 setTimeout(function () {
        this.getPosts();
     }.bind(this), 2000);

Upvotes: 2

Related Questions