In my example, i have two arrays. First array - values, second - zeros for incremental counter. Any new item have own counter button for him. But it is not working, and i do not know why. If i push several buttons, i see chaotioc behavior in arrays
- How to repair it?
How to do counter function without button?
Example: if i loading page, i see 3 elements. Counters begining counting from 0. After 10 seconds i add new element. Old counters continue to work, but counter in new element start from 0.
new Vue({
el: '#page',
data: {
arr: [1, 2 ,3],
count: [0, 0 ,0]
},
methods: {
addEll: function() {
this.arr.push(this.arr.length + 1);
this.count.push(0);
},
incrementio: function(val) {
interval = setInterval(() => {
Vue.set(this.count, this.count[val], 0);
this.count[val]++;
}, 1000);
},
},
computed: {
visibleList: function(){
return this.arr;
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="page">
<button v-on:click="addEll">Add element</button>
{{ arr }}
{{ count }}
<ul>
<li v-for="(item, index) in visibleList">
{{item}}
<button v-on:click="incrementio(index)">Counter: {{count[index]}}</button>
</li>
</ul>
</div>