Run the code and look in the console in your browser. You will see function "formatName()" is called many times. Why? I dont update data of race. But if i change function "amIStarted()" to "return start < 5", then it will be executed 2 times, which is correct. (sorry my english)
https://jsfiddle.net/a496smx2/48/
var stopwatch = new Vue({
el: "#stopwatch",
data: {
time: 1
},
created: function() {
setInterval(function(){
stopwatch.time++;
}, 1000);
}
})
var race = new Vue({
el: "#race",
data: {
startList: [
{name: "John", start: 3},
{name: "Mike", start: 7},
{name: "Gabe", start: 333},
],
},
methods: {
amIStarted: function(start) {
return start < stopwatch.time;
},
formatName: function(name) {
console.log("I was called.")
return 'Mr. '+name;
}
}
});
<div id="stopwatch" ><h4>Time: <span class="gold" >{{time}}</span></h4></div>
<small>Yellow color means the person started</small>
<div id="race" >
<div v-for="oneStart in startList" :class="{gold : amIStarted(oneStart.start)}" >
{{formatName(oneStart.name)}} will start when time is more then {{oneStart.start}}
</div>
<br><br>
</div>
