【问题标题】:Repeating function until the screen is pressed重复功能直到屏幕被按下
【发布时间】:2017-03-27 18:17:21
【问题描述】:

我正在使用这个 src="http://code.responsivevoice.org/responsivevoice.js" (TTS),我想循环播放 3 条消息,直到用户点击屏幕上的某个位置。

播放3条消息,它们之间有某种间隔,第一个..间隔..第二个....间隔...第三个...重复直到点击,他们一直重复直到屏幕被按下,有什么想法吗?试过了,但失败了

var text1 = "This is the first message";
        var text2 = "This is the second message";
        var text3 = "This is the third message";
        var text4 = "This is the fourth message";
    function toggleThem()
{
    window.setTimeout(function(){
            responsiveVoice.speak(text1);
        }, 200);
         window.setTimeout(function(){
            responsiveVoice.speak(text2);
        },2000 );
         window.setTimeout(function(){
            responsiveVoice.speak(text3);
        }, 2500);
}
toggleThem();

【问题讨论】:

  • 不要使用超时,使用事件
  • 为什么不按条件setInterval和removeInterval

标签: javascript jquery html responsivevoice


【解决方案1】:

您可以使用setIntervalclearInterval 使您的语音文本循环,直到单击窗口:

var text1 = "This is the first message";
var text2 = "This is the second message";
var text3 = "This is the third message";
var text4 = "This is the fourth message";

function speak (text) {
  console.log('Speaking: ', text)
  responsiveVoice.speak(text)
}

function toggleThem() {
  setTimeout(speak, 200, text1)
  setTimeout(speak, 2000, text2)
  setTimeout(speak, 2500, text3)
  setTimeout(speak, 3000, text4)
}

var interval = setInterval(toggleThem, 3500)

window.addEventListener('click', function handler() {
  clearInterval(interval)
  this.removeEventListener('click', handler)
})
<script src="http://code.responsivevoice.org/responsivevoice.js"></script>

【讨论】:

  • 真的没用,我的目标是当用户按下屏幕(任何地方)时立即停止说话。在播放时它应该播放所有写的文本
猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多