Javascript onoff按钮在聊天机器人中不工作

Javascript onoff按钮在聊天机器人中不工作,javascript,text-to-speech,chatbot,Javascript,Text To Speech,Chatbot,我是Javascript新手,我想为一个网站构建一个使用语音识别和文本到语音的聊天机器人。我按了一个“开-关”按钮,但不起作用 我试过: while (value == "on") { utterance.volume = 1; } 也 但还是有问题。有什么帮助吗 function onoff(){ currentvalue = document.getElementById('onoff').value; if(currentvalue == "on"){

我是Javascript新手,我想为一个网站构建一个使用语音识别和文本到语音的聊天机器人。我按了一个“开-关”按钮,但不起作用

我试过:

while (value == "on") { 
  utterance.volume = 1; 
}

但还是有问题。有什么帮助吗

function onoff(){
    currentvalue = document.getElementById('onoff').value;
    if(currentvalue == "on"){
        document.getElementById("onoff").value="on";
    }else{
          document.getElementById("onoff").value="off";
    }
 return currentvalue;
}



function speak(string){
var soundonoff = onoff();
    var utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis.getVoices().filter(function(voice) 
    {return voice.name == "Alex";})[0];
    utterance.text = string;
    utterance.lang = "en-US"; 
    if (soundonoff == "on"){
    utterance.volume = 1; //0-1 interval
    }else{
    terance.volume = 0;
    }
    utterance.rate = 0.8;
    utterance.pitch = 1; //0-2 interval
    speechSynthesis.speak(utterance);

}

为此,window.speechSynthesis有暂停和恢复方法

停止:

speechSynthesis.pause(); // pauses utterances being spoken
见:

恢复:

  speechSynthesis.resume();  /// resumes utters ,

取消停车

speechSynthesisInstance.cancel();

根据您的条件,您可以调用这些方法

有关其他方法,请参见

更新:-

这是粗略的代码,根据您的要求进行更改

HTML:

<input type="checkbox" id="onoff" name="onoff" >On/Off<br>
<br/>
<input type="button" name="start" onclick="speak();" value ="start" > On/Off
 <br>
脚本:

 <script>

  var synth = window.speechSynthesis;

   function speak(){
      var utterThis = new SpeechSynthesisUtterance('My pleasure, poke me up if you need more info.'); // 
      synth.speak(utterThis);
    }


 //// on check box change event call this 

   $('#onoff').change(function() {

      if($("#onoff").prop('checked') == true){
       speechSynthesis.pause();
      /// now I dont know you want to pause or stop, change it according to you 
       ///requirment 
       }  
     else {
         speechSynthesis.pause();
       }
  });
  </script>

GITHUB-

感谢您的帮助,但问题实际上并不在于如何停止或暂停演讲。问题在于按钮未连接到onclick事件,因为无论按钮在开始时是否有值,它都会保持打开状态,而无论按钮是否关闭,它都会保持关闭状态。你知道如何解决这个问题吗?哦,谢谢,我正在这个网站上工作:在这里你可以找到聊天机器人页面的全部代码,我无法在这里共享,因为它的长度超过200letter@WaelAlMasri我没有得到你想要的代码,但我用一个简单的例子更新了我的答案,关于复选框更改事件,希望你能从这个答案中得到一些想法,如果它解决了你的问题,我真的很感激,但是即使在你的代码中,如果你勾选了这个框,它也会继续说话,也许是浏览器?所以我想做的是,我想要一个静音按钮,以防用户不想听到任何声音,这样他就可以使聊天机器人静音,但正如我注意到的,当页面加载时,按钮会取一个值,并且无论你做了多少操作,都不会改变check@WaelAlMasri如果复选框不起作用,则尝试单击按钮,如果我尝试了相同的示例,它对我起作用,请参阅是否添加了jquery库,有关错误,请参阅控制台
 <script>

  var synth = window.speechSynthesis;

   function speak(){
      var utterThis = new SpeechSynthesisUtterance('My pleasure, poke me up if you need more info.'); // 
      synth.speak(utterThis);
    }


 //// on check box change event call this 

   $('#onoff').change(function() {

      if($("#onoff").prop('checked') == true){
       speechSynthesis.pause();
      /// now I dont know you want to pause or stop, change it according to you 
       ///requirment 
       }  
     else {
         speechSynthesis.pause();
       }
  });
  </script>