想要事件实时触发,但是键盘操作就一直在触发,比较影响性能,所以就需要设置定时器,在每次输入的时候,都清除这个定时器,只有在一秒盘内没有任何输入操作的时候,这个定时器才会生效。
代码如下:
1、template中定义的input输入框
<input type="text" class="predict_score" v-model="score" maxlength="3" @input="handleInput" @click="toggleInput" ref="scoreInput" placeholder="点击请输入成绩">
2、script中的data定义变量,定时器TimeId为-1
TimeId: -1
3、method中定义方法:
handleInput () {
clearTimeout(this.TimeId)
this.TimeId = setTimeout(() => {
this.gotoBlur() // 定时器生效时执行的方法
}, 1000)
console.log('分数:', this.score)
},