Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用纯Js在每个li上应用函数_Javascript_Loops - Fatal编程技术网

Javascript 使用纯Js在每个li上应用函数

Javascript 使用纯Js在每个li上应用函数,javascript,loops,Javascript,Loops,我已经完成了一个函数,它使用SpeechAPI读取一个元素的内容,我有一个这样的新闻列表 <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]

我已经完成了一个函数,它使用SpeechAPI读取一个元素的内容,我有一个这样的新闻列表

<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>
我如何让它以很小的间隔阅读每一个li?我不想使用jquery之类的工具。 但是我做不到。怎么样

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);
}
函数speak(){
var i=0,
li=document.getElementById('news').getElementsByTagName('li'),
间隔=设置间隔(函数(){
如果(i

怎么样

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);
}
函数speak(){
var i=0,
li=document.getElementById('news').getElementsByTagName('li'),
间隔=设置间隔(函数(){
如果(i


这里有一种方法,每行之间有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);

这里有一种方法,每行之间有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);

以下是我对这个问题的看法:

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);
    }
}
函数speak(){
var li=document.getElementById('news').getElementsByTagName('li'))
,i=0;
li=[].slice.call(li,0);
(函数speakItem(){
speechapi.speak(li[i++].innerHTML,“male”);
i
[].slice.call(li,0)
将li元素的实时列表转换为一个简单的JS数组,从而提高处理效率


读取每个项目,然后,如果有更多项目,则在说出下一个项目之前,将开始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);
    }
}
函数speak(){
var li=document.getElementById('news').getElementsByTagName('li'))
,i=0;
li=[].slice.call(li,0);
(函数speakItem(){
speechapi.speak(li[i++].innerHTML,“male”);
i
[].slice.call(li,0)
将li元素的实时列表转换为一个简单的JS数组,从而提高处理效率

读取每个项目,然后,如果有更多项目,则在说出下一个项目之前,将开始1秒的间隔。这允许长项目和短项目之间具有恒定的间隔