I’m working through R Murphy’s JQuery Fundamentals and am stuck on a snippet of code in 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']();
},
It seems to me that inside getItem(), in the line…
$items[(trav == 'next') ? 'first' : 'last']();
…trav is never not going to be ‘next’. i.e. Nowhere else in the code do we set ‘trav’ to anything but ‘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?
It’s clearly being used for
prev.