以下是修改后的代码:
<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 的值,使其更适合你的应用场景。
网友回复
js如何流式输出ai的回答并折叠代码块,点击代码块右侧可预览代码?
ai大模型如何将文章转换成可视化一目了然的图片流程图图表?
大模型生成html版本的ui原型图和ppt演示文档的系统提示词怎么写?
rtsp视频直播流如何转换成websocket流在h5页面上观看?
为啥coze会开源工作流agent coze studio?
如何检测网页是通过收藏夹打开的?
python如何实现类似php的http动态脚本请求处理响应代码?
js如何实现类似php的http动态脚本请求处理响应代码?
trae与solo有啥区别不同?
vue如何让ai动态生成问卷调查多步骤表单式收集基础信息自动规划执行任务?