如何在javascript / jquery中使用setInterval来处理数组?

时间:2013-05-03 20:53:34

标签: javascript jquery function variables setinterval

我想在setInterval中运行一个函数,并在每次setInterval运行时更改该函数的变量,这是我到目前为止所拥有的:

$pages = new Array();
$pages.push("home.html","drp.html","drs.html","dffi.html","dci.html","dcp.html","drma.html","construction.html","orgChart.html");
$a = 0;
setInterval(function(){
    $getPage = $pages[$a]
    $(this).getPageToLeft();
    $a++
    alert($a);  
},5000);

问题是每次setInterval运行时$ a都会重新设置为0,每次setInterval运行时我都需要增加它。

2 个答案:

答案 0 :(得分:1)

var pages = new Array();

pages.push("home.html","drp.html","drs.html","dffi.html","dci.html","dcp.html","drma.html","construction.html","orgChart.html");
var a = 0;
setInterval(function(){
    getPage = pages[a]
    //$(this).getPageToLeft();
    a++
    alert(a)
},5000);

这应该可以修复您的代码。

答案 1 :(得分:0)

好的问题是我使用了错误的操作员,所以我希望这有助于减少道路:

// Auto Start
var pages = new Array();

pages.push("home.html","drp.html","drs.html","dffi.html","dci.html","dcp.html","drma.html","construction.html","orgChart.html");
var a = 1;
var int=self.setInterval(function(){flip()},5000);
function flip(){
    if (a === 9){
    a = 0;
    $getPage = pages[a];
    $(this).getPageToLeft();
    a++;
    }else{
    $getPage = pages[a];
    $(this).getPageToLeft();
    a++;
    }   
}
$(document).ready(function(e) {
    $("#getDRP, #getDRS, #getDFFI, #getDCI, #getDCP, #getDRMA, #getConstruction, #getOrgCharts, .leftButton, .rightButton").click(function(){
        int=window.clearInterval(int)
    });
});