以下是修改后的代码:
<view class="flex flex-direction">
<button class="cu-btn lg margin-tb-sm"
:class="isRecording ? 'bg-red' : 'bg-blue'"
@touchstart="handleTouchStart"
@touchend="stopRecording"
style="height: 80px;">
<text class="cuIcon-voice"></text>
{{isRecording ? '松开结束' : '按住不放开始'}}
</button>
</view> 然后在你的JavaScript/Vue方法部分添加以下代码:
data() {
return {
isRecording: false,
lastTouchTime: 0,
touchThreshold: 300 // 设置触发间隔阈值,单位毫秒
}
},
methods: {
handleTouchStart(e) {
const now = Date.now();
// 检查距离上次触发的时间是否超过阈值
if (now - this.lastTouchTime > this.touchThreshold) {
this.lastTouchTime = now;
this.startRecording(e);
}
},
startRecording(e) {
// 原来的录音开始逻辑
if (!this.isRecording) {
this.isRecording = true;
// 其他录音开始代码...
}
},
stopRecording(e) {
// 录音结束逻辑
if (this.isRecording) {
this.isRecording = false;
// 其他录音结束代码...
}
}
} 解决方案说明添加了一个新的方法 handleTouchStart 来处理 touchstart 事件使用时间戳来跟踪上次触发的时间设置了一个阈值(这里是300毫秒),只有当两次触发的间隔大于这个阈值时,才会真正执行录音开始的逻辑这样可以有效防止用户快速点击导致的多次触发问题你可以根据实际需求调整 touchThreshold 的值,使其更适合你的应用场景。
网友回复


