0

スピーチAPIを使用して要素のコンテンツを読み取る機能を実行しました。このようなニュースのリストがあります。

<ul id="news">
    <li>
How Instagram Is Winning Gold at the 2012 Olympics [INFOGRAPHIC]
   </li><li>
      Flickr Photo of Insect Identified As Never-Before-Seen Species [VIDEO]
   </li><li>
      The Upside to the End of the Olympics [COMIC]
   </li><li>
      40 Digital Media Resources You May Have Missed
   </li><li>
      19 Resources to Improve Your Photo and Video Skills
   </li><li>
      Top 10 Twitter Pics of the Week
   </li><li>
      Can Employees Be Trusted to Work From Home? [INFOGRAPHIC]
   </li><li>
      Capture Ghosts at Foursquare Spots With ‘Ghostbusters’ iOS Game
   </li><li>
      Dog Is Terrified of Low-Fat Lamb Jerky [VIDEO]
   </li><li>
      5 Tips to Take Food Photos Good Enough to Eat
   </li><li>
      The 10 Most Stunning Images of Mars in History
   </li><li>
      The Many Looks of Spiderman Over the Past 50 Years [INFOGRAPHIC]
   </li><li>
      Top Ten GIFs of the Week
   </li><li>
      Romney and Obama Both Called Their Veeps the ‘Next President’ [VIDEO]
   </li><li>
      Paul Ryan Visited Facebook, Met Zuckerberg [PICS]
   </li><li>
      Romney’s Logo Looks Like Toothpaste [PICS]
   </li><li>
      Paul Ryan, Romney’s VP Pick, Has Sizable Online Presence
   </li><li>
      Get Back in Kitchen With This Specialized Recipe Site
   </li>

</ul>

現在、この関数を使用してテキストを読み取っています。

function speak() {
    //speechapi.speak(document.getElementById('start').innerHTML,"male");
    speechapi.speak(document.getElementById('start').innerHTML,"male");

}

間隔を空けて各liを読み取らせるにはどうすればよいですか?jqueryなどは使いたくない。しかし、私はそれをすることができません。

4

3 に答える 3

1

これが問題に対する私の見解です:

function speak() {
    var li = document.getElementById ('news').getElementsByTagName ('li')
      , i = 0;

    li = [].slice.call (li, 0);

    (function speakItem() {
      speechapi.speak(li[i++].innerHTML, "male");
      i < li.length && window.setTimeout (speakItem, 1000);
    }
}

[].slice.call (li, 0)、li要素のライブリストを単純なJS配列に変換し、より効率的な処理を実現します。

各項目が読み取られ、さらに項目がある場合は、次の項目を話す前に1秒の間隔が開始されます。これにより、それぞれの間に一定の間隔がある長いアイテムと短いアイテムが可能になります。

于 2012-08-12T04:19:10.210 に答える
1

どうですか

function speak() {
    var i = 0,
    li = document.getElementById('news').getElementsByTagName('li'),
    interval = setInterval(function(){
        if (i < li.lenght){
            speechapi.speak(li[i].innerHTML,"male");
            i++;
        }
        else{
            clearInterval(interval);
        }
    }, 5000);
}

https://developer.mozilla.org/en-US/docs/DOM/document.getElementsByTagName

https://developer.mozilla.org/en-US/docs/DOM/document.getElementById

https://developer.mozilla.org/en-US/docs/window.setInterval

https://developer.mozilla.org/en/DOM/window.clearInterval

于 2012-08-12T03:59:25.573 に答える
1

これを行う1つの方法は、各行の間に2秒の遅延があります。

var news = document.getElementById('news').getElementsByTagName('li');

var newsItem = 0;
var speechInterval = setInterval(function(){
    speechapi.speak(news[newsItem].innerHTML, "male");
    if (++newsItem === news.length){
        window.clearInterval(speechInterval);
    }
}​, 2000);

デモ、<p>話す代わりにタグにテキストを挿入する

于 2012-08-12T04:02:33.047 に答える