I'm working through R Murphy's JQuery Fundamentals and am trying to make sense of her solution: slideshow.js.
She's created the following function (I've removed some of the navigation code in order to focus on the core functionality of fading images in and out):
fadeCallback = function() {
if (manualMode) { return; }
var $this = $(this),
$next = getItem($this, 'next'),
num = $this.prevAll().length + 1;
// set the timeout for showing
// the next item in 5 seconds
timeout = setTimeout(function() {
showItem($this, $next);
}, 5000);
};
Inside fadeCallback she calls getItem() to get the next sibling of $this:
getItem = function($item, trav) {
var $returnItem = $item[trav]();
return $returnItem.length ?
$returnItem :
$items[(trav == 'next') ? 'first' : 'last']();
},
I'm getting lost by her use of 'next' in the second argument to getItem(). Is this a roundabout way of calling the .next() jQuery function on the first argument, '$(this)'?
If so, why not just call the function directly? i.e. $item.next();?
Follow-up question
Maybe I've been looking at this code too long but it seems to me that inside getItem(), in the line:
$items[(trav == 'next') ? 'first' : 'last']();
trav is never not 'next'. Yet by virtue of the code testing whether trav equals 'next' it implies that there are cases where trav != 'next'. But I can't identify any such case.
What am I not seeing?