首页 > 解决方案 > 即使您在输入字段中有多个字母/数字(rfid 代码),如何让它只说一次?

问题描述

每当我点击有 10 个字符代码的 RFID 时,代码都会循环 10 次,我怎样才能让它只说一次然后取消下一个?

  $(function(){
  if ('speechSynthesis' in window) {
    speechSynthesis.onvoiceschanged = function() {
      var $voicelist = $('#voices');

      if($voicelist.find('#voices').length == 0) {
        speechSynthesis.getVoices().forEach(function(voice, index) {
          var $option = $('#voices')
          .val(index)
          .html(voice.name + (voice.default ? ' (default)' :''));

          $voicelist.append($option);
        });

      }
    }

    $('#rfid').keypress(function(){
      var text = document.getElementById("res").innerHTML; 
      var msg = new SpeechSynthesisUtterance();
      var voices = $('#voices');
      msg.voice = voices[$('#voices').val()];
      msg.rate = 10 / 10;
      msg.pitch = 1;
      msg.text = text;


      speechSynthesis.speak(msg);

    })
  }
});

标签: javascriptjqueryhtml

解决方案


推荐阅读