0

我有这个每 5 秒运行一次的函数。每次函数运行时,我都想使用数组中的下一个值,直到我到达数组的末尾,然后停止该函数的运行。每次函数运行时,我当前的代码只使用数组的第一项!数组索引永远不会以某种方式增加!

谁能告诉我如何在数组中使用不同的项目并在到达数组末尾时停止运行该函数?

<head>

<script type="text/javascript">


window.setInterval(function(){

episode=["http://www.asite.com/video1&title=video1 title","http://www.asite.com/video2&title=video2 title"];
m=0;
//alert ("wow");
$.ajax(
{


    type: 'GET',

        url: './getit.php?url='+episode[m],

            success: function (good)
            {
              //handle success

                  //alert(good)
                 $('#myDiv2').append(good)

             $("textarea[name='content']").html($("textarea[name='content']").html()+"\n"+good);
                m++;

            },
            failure: function (bad)
            {
               //handle any errors

                alert(bad)

            }




});

}, 5000);

</script>
</head>

<body>
<div id="myDiv2"></div>
<textarea name="content" rows="30" cols="150" ></textarea>

</html>
4

1 回答 1

1

m在函数之外设置变量:

m = 0;
window.setInterval(function(){ //do more stuff now

然后运行检查以验证m不大于数组长度。

if (m < episode.length) {
    //m is still within the array length, do stuff
} else {
    return; //m is too big, bail
}
于 2013-06-04T17:25:43.763 回答