<button @click="toggleLottery">{{ lotteryButton }}</button>
<p v-if="prize">{{ prize }}</p>
<script>
export default {
name: 'Home',
data() {
return {
lotteryButton: '点击抽奖',
prize: null,
isLotteryRunning: false,
prizes: ['一等奖', '二等奖', '三等奖', '谢谢参与']
}
},
methods: {
toggleLottery() {
if (this.isLotteryRunning) {
this.isLotteryRunning = false;
this.lotteryButton = '点击抽奖';
} else {
this.isLotteryRunning = true;
this.lotteryButton = '正在抽奖请稍后';
this.startLottery();
}
},
startLottery() {
let index = 0;
let interval = setInterval(() => {
index = (index + 1) % this.prizes.length;
this.prize = this.prizes[index];
}, 100);
let stopTime = this.isLotteryRunning ? (Math.floor(Math.random() * 5000) + 5000) : 0;
setTimeout(() => {
clearInterval(interval);
this.isLotteryRunning = false;
this.lotteryButton = '点击抽奖';
}, stopTime);
}
}
}
</script>