简体   繁体   English

使用Titanium我想在单击下一个按钮之后逐个循环遍历数组,然后使用上一个按钮向后循环

[英]Using Titanium I want to loop through an array 1 by 1 after clicking the next button and backwards using the previous button

//Using Titanium I want to loop through an array 1 by 1 after clicking the next button and backwards //using the previous button but everytime I do it it loops through the entire array... here is what I //have so far: //使用Titanium我想在单击下一个按钮后向后遍历一个数组,并使用上一个按钮向后//遍历数组,但是每次执行此操作时,它都会遍历整个数组...这是我到目前为止所要做的:

var fishArray = ["tang", "clownfish", "rabbit fish", "trigger fish", "blue green chromis", "angelfish"];
var index = 0;

var forwards = function(){

for(var i = index, j=fishArray.length; i<j; index++){
    var fishLabel = Ti.UI.createLabel({
        text: fishArray[index],
        backgroundColor: "#fff",
        width: 250,
        height: 200,
        top: view1.height + spacing,
        borderRadius: 5,
        borderWidth: 1
    });
    win1.add(fishLabel);
}
};


button1.addEventListener("click", backwards);
button2.addEventListener("click", forwards);

maybe index++ is missing, you have to increment/decrement it 也许缺少index++ ,您必须递增/递减它

and the loop looks strange 循环看起来很奇怪

try this 尝试这个

for(var i = index; i<fishArray.length; i++)

and change 并改变

text: fishArray[index],

to

text: fishArray[i],

whole code: 整个代码:

var fishArray = ["tang", "clownfish", "rabbit fish", "trigger fish", "blue green chromis", "angelfish"];
var index = 0;

var labels = function(){
    for(var i = index; i<fishArray.length; i++){
        var fishLabel = Ti.UI.createLabel({
            text: fishArray[i],
            backgroundColor: "#fff",
            width: 250,
            height: 200,
            top: view1.height + spacing,
            borderRadius: 5,
            borderWidth: 1
        });
        win1.add(fishLabel);
    }
};
var backwards = function(){
    labels();
    index--;
};
var forwards = function(){
    labels();
    index++;
};


button1.addEventListener("click", backwards);
button2.addEventListener("click", forwards);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM