<template> <div> <div v-for="(item, index) in items" :key="index" class="row"> <button :disabled="!start || !item.checked" @click="clickButton(index, item.delay)" :class="{ active: item.active }" > Button {{ index + 1 }} <span v-if="item.active" class="click-animation"></span> </button> <input type="checkbox" v-model="item.checked" :disabled="!start"/> </div> <button :class="{ start: start, stop: !start }" @click="startStop">{{ start ? 'Stop' : 'Start' }}</button> </div> </template> <script> export default { data () { return { items: [ {checked: false, delay: 100, active: false}, {checked: false, delay: 200, active: false}, {checked: false, delay: 300, active: false}, {checked: false, delay: 400, active: false} ], start: true, intervalIds: [] } }, methods: { clickButton (index, delay) { this.items[index].active = true setTimeout(() => { console.log(Clicked button ${index + 1}) this.items[index].active = false }, 100) if (this.items[2].checked) { setTimeout(() => { console.log('Clicked button 1 of the third row') }, delay) setTimeout(() => { console.log('Clicked button 3 of the third row') }, delay + 300) } }, startStop () { this.start = !this.start if (this.start) { this.intervalIds.forEach((id) => clearInterval(id)) } else { this.intervalIds = this.items.map((item, index) => { if (item.checked) { return setInterval(() => { this.clickButton(index, item.delay) }, item.delay) } else { return null } }) } } } } </script>如何将第一行按钮规则改成每次点击开始后每0.1秒执行
时间: 2024-01-15 08:02:49 浏览: 66
牛牛视频答题小程序 <template name="tmpl-15-container">
要将第一行按钮规则改成每次点击开始后每0.1秒执行,可以修改clickButton方法中的setTimeout函数,将延迟时间设为0.1秒,并将console.log语句移入setTimeout函数中。具体修改如下:
```
clickButton (index, delay) {
this.items[index].active = true
setTimeout(() => {
console.log(`Clicked button ${index + 1}`)
this.items[index].active = false
}, 100)
if (this.items[2].checked) {
setTimeout(() => {
console.log('Clicked button 1 of the third row')
}, delay)
setTimeout(() => {
console.log('Clicked button 3 of the third row')
}, delay + 300)
}
if (index === 0) {
setTimeout(() => {
this.clickButton(index, delay)
}, 100)
}
}
```
这样修改后,当点击第一行按钮时,会在开始后立即执行一次clickButton方法,并且每0.1秒执行一次,直到该按钮被停止或取消勾选。
阅读全文