v-for vuejs how to charge with timer

Asked

Viewed 126 times

4

How do I get the list display to be paused,

example wait 1 second display:

1 
+ 1 segundo
2
+ 1 segundo
3
<div>
  <span v-for="n in 10">{{ n }} </span>
</div>

I used setTimeout in created but it didn’t work.

  • I believe this can help you https://medium.com/byteconf/building-a-pomodoro-timer-with-vue-js-on-codepen-ec9d1d53e833

1 answer

7


To control this you need to change what Vue uses to iterate, that is to create an array and change the array at the speed you need to have the desired effect:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var demo = new Vue({
  el: '#demo',
  data() {
    return {
      arr: arr.slice(0, 1),
      showUpTo: 1,
    }
  },
  mounted() {
    const timer = setInterval(() => {
      this.showUpTo++;
      this.arr = arr.slice(0, this.showUpTo);
      if (this.showUpTo === arr.length) clearInterval(timer);
    }, 1000);
  }
});
<script src="http://vuejs.org/js/vue.min.js"></script>
<div id="demo">
  <div>
    <p v-for="n in arr">{{ n }} </p>
  </div>
</div>

The same thing but with property computed:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var demo = new Vue({
  el: '#demo',
  data() {
    return {
      showUpTo: 1,
    }
  },
  computed: {
    arr() {
      return arr.slice(0, this.showUpTo);
    }
  },
  mounted() {
    const timer = setInterval(() => {
      this.showUpTo++;
      if (this.showUpTo === arr.length) clearInterval(timer);
    }, 1000);
  }
});
<script src="http://vuejs.org/js/vue.min.js"></script>
<div id="demo">
  <div>
    <p v-for="n in arr">{{ n }} </p>
  </div>
</div>

  • 1

    Excellent answer, I kept trying to imagine what would be an answer to this question, almost came to say that gives way that he was trying it would be impossible.

  • 1

    +1 I have come in the hope of creating an answer, but I do not see a clearer and simpler form than your :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.