Check out this VueJS code snippet:
new Vue({
el: '#app',
data: {
tiles: [
{ isActive: false },
{ isActive: false },
{ isActive: false },
{ isActive: false },
{ isActive: false }
]
},
methods: {
startWithLoop: function() {
console.log("startWithLoop");
for(var i = 0; i < 10000; i++ ) { this.blink() };
},
startWithInterval: function() {
console.log("startWithInteral");
setInterval(this.blink);
},
blink: function(){
console.log("blink");
var index = Math.floor(Math.random() * this.tiles.length);
this.tiles[index].isActive = !this.tiles[index].isActive;
}
}
})
By calling the startWithInterval
method, you can witness constant changes in the view as the state of tiles
fluctuates.
However, if you use the startWithLoop
method, there won't be visible changes in the view until the loop completes its iterations.
You can access the JSFiddle example here.
Is there a way to trigger immediate view updates during each step of the loop?