2017-05-30 148 views
2

我想突出顯示特定段落/列表及其相應的語音。ResponsiveVoice爲多個話語只撥打一次電話號碼

在respondvoice.js中是否有任何回調?我得到了一個回調函數,但它不工作。

每當我把控制檯,而不是突出顯示,它只產生一個而不是三個。

我覺得onend只對第一次以後打電話。但它應該爲所有對工作/ UL

請幫我..

我的代碼: -

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="http://code.responsivevoice.org/develop/1.5.2/responsivevoice.js"></script> 

<script> 
var currentDataValue =0; 
$(document).ready(function(){ 
    $("#1").click(function(){ 
     $("p,ul").each(function(index, element){ 
      responsiveVoice.speak($(this).text(),$('UK English Female').val(),{ 
     pitch: .7, 
     onend: function() { 
      $(this).css("backgroundColor", "yellow"); 

     } 
     }); 

     }); 
    }); 
}); 

$(document).ready(function(){ 
    $("#2").click(function(){ 
     responsiveVoice.pause(); 
    }); 
}); 
$(document).ready(function(){ 
    $("#3").click(function(){ 
     responsiveVoice.resume(); 
    }); 
}); 
$(window).load(function(){ 
    $("#4").click(function(){ 
     responsiveVoice.cancel(); 
    }); 
}); 
</script> 
</head> 
<body> 

<h2>This is a heading</h2> 
<p>This is a paragraph.</p> 
<ul> 
<li>this unoder list </li> 
</ul> 
<p id="test">This is another paragraph.</p> 
<button id="1">start</button> 
<button id="2">pause</button> 
<button id="3">resume</button> 
<button id="4">stop</button> 
</body> 
</html> 

回答

0

好,這個ResponsiveVoice是一個帶有錯誤的商業庫,它爲回調設置單個變量,因此它只調用最後一次配置的回調,而不是所有的回調。您可以修改過程的圖書館,也可以解決調用的項目一個接一個這樣的:

$("#1").click(function(){ 

    var elements = $("p, ul"); 
    speak = function(i, elements) { 
     responsiveVoice.speak($(elements[i]).text(),$('UK English Female').val(),{ 
      pitch: .7, 
      onend: function() { 
       $(elements[i]).css("backgroundColor", "yellow"); 
       if (i < elements.length) { 
        speak(i + 1, elements); 
       } 
      } 
     }); 
    }; 
    speak(0, elements); 
}); 

我會簡單地使用Chrome的API,而不是像這樣:

var currentDataValue =0; 
$(document).ready(function(){ 
    $("#1").click(function(){ 
    var voice = speechSynthesis.getVoices().filter(function(voice){return voice.name == 'Allison'; })[0]; 
     $("p,ul").each(function(index, element) { 
      u = new SpeechSynthesisUtterance($(this).text()); 
      u.voice = voice; 
      u.onend = (function() { 
        console.log("Here"); 
        $(this).css("background-color", "yellow"); 
      }).bind($(this)); 
      speechSynthesis.speak(u); 
     }); 
    }); 
}); 
+0

感謝響應。語音正確地撥打但問題僅僅是一個回調,並不是爲每個para/ul撥打電話。 U也可以通過$(this).css(「backgroundColor」,「yellow」)來檢查;與一些日誌(打印)。它只打印一次,但在每個para/ul的語音合成後應該是3次。 –

+0

好吧,我更新了答案 –

+0

因爲他們(響應式語音)處理了很多情況。例如: - 大型會議(超過200字),語音質量,瀏覽器兼容性等。所以我使用它們。我會像你一樣(這對演示有效),那麼我必須處理很多情況。 –