Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

javascript - Javascript备用功能在onclick上运行(Javascript Alternate functions run onclick)

So I have this speech to text typing feature that is triggered upon pressing a microphone icon.(因此,我具有语音输入文本功能,该功能是在按下麦克风图标时触发的。)

The feature works.(该功能有效。) My problem is that I want the same icon to do 2 things.(我的问题是我希望同一图标执行2项操作。) 1st click it will start hearing for voice.(第一次单击它将开始听到声音。) 2nd click will stop hearing for voice.(第二次单击将停止听到声音。) I basically want the function onclick to alternate between 2 functions, Start and Stop.(我基本上希望功能onclick在两个功能之间交替,即开始和停止。) How do I do it?(我该怎么做?) Below is what I got currently.(以下是我目前得到的。) That only works to START the recording How do I fit recognition.stop() into the same click function?(那仅对开始录制有效。如何将recognition.stop()放入同一单击函数?) $('#micButton').on('click', function () { recognition.start(); //this is to start recording }); Below this is what I thought would work (it doesn't), to alternate between start and stop.(在此之下,我认为可以在开始和停止之间进行切换(不起作用)。) $('#micButton').on('click', function () { recognition.start(); }, function () { recognition.stop(); } ); To summarise, how do I get my onclick to alternate between start and stop?(总而言之,如何让我的onclick在开始和停止之间切换?)   ask by Coben Yap translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll need a flag of some sort to check to see if voice is currently being listened for:(您将需要某种标记来检查当前是否正在收听语音:)

let currentlyListening = false; $('#micButton').on('click', function () { if (!currentlyListening) { recognition.start(); //this is to start recording } else { recognition.stop(); } currentlyListening = !currentlyListening; }); It'd probably be good to also provide an indicator to the user the current state as well, if recognition doesn't already do that - eg, change or add an image that shows a red dot while currentlyListening is true .(如果recognition还没有做到,最好也向用户提供当前状态的指示符,例如,更改或添加在currentlyListeningtrue时显示红点的图像。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...