<html>
<head>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<input type="button" value="开始" @click="start">
<input type="button" value="结束" @click="stop">
<p>{{ msg }}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: "大不自多,海纳江河~ 唯学无际, 际于天地~",
intervalId: null //在data上定义定时器 Id
},
methods: {
start(){
if(this.intervalId != null){
return;
}
//箭头表达式
this.intervalId = setInterval(() => {
// console.log(this.msg)
var start = this.msg.substring(0, 1)
var end = this.msg.substring(1)
this.msg = end + start
}, 400)
},
stop(){
clearInterval(this.intervalId);
this.intervalId = null;
}
}
})
</script>
</body>
</html>
来源:CSDN
作者:_WWWZP
链接:https://blog.csdn.net/Nase_Mitsuki/article/details/103800250