~~~
//HTML
<div id="app">
<input type="button" value="浪起來" @click="lang">
<input type="button" value="低調" @click = "stop">
<h4>{{msg}}</h4>
</div>
~~~
~~~
//JS
new Vue({
el: '#app',
data: {
msg: "猥瑣發育,別浪~~~",
// 給定時器一個名字
timer: null
},
methods: {
lang: function () {
if (this.timer!=null) {
return
} else {
this.timer = setInterval(() => {
var start = this.msg.substring(0, 1);
var end = this.msg.substring(1);
this.msg = end + start;
}, 300)
}
console.log(this.timer);
},
stop(){
clearInterval(this.timer);
this.timer = null;
}
}
})
~~~