到达数据对象末尾时停止迭代

问题描述:

使用 TweenMax 库,当到达数据对象的末尾而不是循环时,我无法杀死或停止delayedCall,因为它是自调用函数.

Using the TweenMax library, I am unable to kill or stop a delayedCall when the end of the data object is reached instead of looping because it is a self calling function.

https://jsfiddle.net/rdzo13cf/6/

在上面的示例中,数据的最后一项没有显示.

In the example above the last item in the data is not displayed.

实际上是因为您在设置iter变量之前先对其进行了递增.这会导致它不显示最后一项,而您要在之后增加:

Actually it's because you are incrementing your iter variable before you set it. Which causes it to not display the last item, you want to increment after:

function setContent() {
    element.html(data[iter].content);
    iter = iter >= data.length-1 ? -1 : iter;
    iter = iter + 1;    
}

基本上,当您要遍历[0,1,2]时,您正在遍历索引[1,2,3].

Basically you are going over indexes [1,2,3] when you want to be going over [0,1,2].

小提琴示例