我正在寻找迭代数组的方法,只输出第一项,再次迭代时,输出第二项。
var array = [1,2,3]
iterate : function () {
$.each(array, function(a,b) {
console.log(a); // This will log 1 and 2 and 3
});
这将记录1,2和3,但我只想先记录1,再次调用iterate
函数时,它将记录2然后记录3
我将iterate
功能分配给按钮(增量按钮)。因此,增量按钮将在首先单击时输出1,然后在再次单击时输出2,然后再次单击时输出3。
答案 0 :(得分:1)
我就是这样做的
var arr = [1,2,3,4]
var index = 0;
function amClicked(){
console.log(arr[index%arr.length]);
index++
if(index == arr.length){
document.getElementById("yo").disabled = true;
}
}

<input type="button" id="yo" onclick="amClicked()" value="yo" />
&#13;
答案 1 :(得分:1)
设置两个const变量来保存最小值和最大值,并声明一个可变变量来保存你的计数:
theta