From 5dec7fb30cf8cfded196eaf6dcde9355c2037884 Mon Sep 17 00:00:00 2001 From: guoqingshan Date: Sun, 2 Aug 2026 12:24:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF=E6=9C=97?= =?UTF-8?q?=E8=AF=BB=E6=94=B9=E4=B8=BA=E5=90=8C=E6=AD=A5=E6=89=8B=E5=8A=BF?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=B9=B6=E5=85=BC=E5=AE=B9cancel=E5=90=8E?= =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- speech.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/speech.js b/speech.js index 2a4232a..d6f11cb 100644 --- a/speech.js +++ b/speech.js @@ -1,5 +1,6 @@ var speechVoices = []; var speechVoicesLoaded = false; +var speechTimer = null; function refreshSpeechVoices() { if (!window.speechSynthesis) return; @@ -36,28 +37,20 @@ function buildSpeechUtterance(text, lang) { } function doSpeak(text, lang) { - if (!window.speechSynthesis || !text) return; + if (!window.speechSynthesis || !window.SpeechSynthesisUtterance || !text) return; window.speechSynthesis.cancel(); - window.speechSynthesis.speak(buildSpeechUtterance(text, lang)); + if (speechTimer) clearTimeout(speechTimer); + var utterance = buildSpeechUtterance(text, lang); + speechTimer = setTimeout(function() { + window.speechSynthesis.speak(utterance); + if (window.speechSynthesis.paused) window.speechSynthesis.resume(); + }, 60); } function speakText(text, lang) { if (!text || !window.speechSynthesis) return; refreshSpeechVoices(); - if (speechVoicesLoaded) { - doSpeak(text, lang); - return; - } - - var attempts = 0; - var timer = setInterval(function() { - attempts++; - refreshSpeechVoices(); - if (speechVoicesLoaded || attempts >= 8) { - clearInterval(timer); - doSpeak(text, lang); - } - }, 80); + doSpeak(text, lang); window.speechSynthesis.getVoices(); } @@ -71,3 +64,12 @@ function initSpeech() { if (document.readyState === 'complete') initSpeech(); else document.addEventListener('DOMContentLoaded', initSpeech); + +function warmSpeechVoices() { + if (!window.speechSynthesis) return; + window.speechSynthesis.getVoices(); + refreshSpeechVoices(); +} + +document.addEventListener('touchstart', warmSpeechVoices, { once: true, passive: true }); +document.addEventListener('pointerdown', warmSpeechVoices, { once: true, passive: true });