A function that returns the next item in a given array. Starts over when the last item is reached.
var next = nextCircularItem(['x', 'y', 'z']);
next(); // x
next(); // y
next(); // z
next(); // x
// etc
function nextCircularItem(arr) {
var len = arr.length;
var current = -1;
return function() {
var next = current + 1;
current = next === len ? 0 : next;
return arr[current];
};
}