Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 4 characters in body
Source Link
David Thomas
  • 254.1k
  • 53
  • 382
  • 421

It's not exactly a pretty answer, but one approach is simply to assess whether there is, or is not, a next() element and re-call the function or perform a different action:

function elFadeIn(elem) {
 elem.fadeIn('slow', function() {
 if ($(this).next().length) {
 elFadeIn($(this).next());
 }
 else {
 alert('finished');
 }
 });
}​

JS Fiddle demo.

And a slightly prettier way of writing that would be:

function elFadeIn(elem, callback) {
 elem.fadeIn('slow', function() {
 var next = $(this).next();,
 action = next.length ? elFadeIn(next) : alert('finished!');
 });
}​

JS Fiddle demo JS Fiddle demo.

It's not exactly a pretty answer, but one approach is simply to assess whether there is, or is not, a next() element and re-call the function or perform a different action:

function elFadeIn(elem) {
 elem.fadeIn('slow', function() {
 if ($(this).next().length) {
 elFadeIn($(this).next());
 }
 else {
 alert('finished');
 }
 });
}​

JS Fiddle demo.

And a slightly prettier way of writing that would be:

function elFadeIn(elem, callback) {
 elem.fadeIn('slow', function() {
 var next = $(this).next();
 action = next.length ? elFadeIn(next) : alert('finished!');
 });
}​

JS Fiddle demo.

It's not exactly a pretty answer, but one approach is simply to assess whether there is, or is not, a next() element and re-call the function or perform a different action:

function elFadeIn(elem) {
 elem.fadeIn('slow', function() {
 if ($(this).next().length) {
 elFadeIn($(this).next());
 }
 else {
 alert('finished');
 }
 });
}​

JS Fiddle demo.

And a slightly prettier way of writing that would be:

function elFadeIn(elem, callback) {
 elem.fadeIn('slow', function() {
 var next = $(this).next(),
 action = next.length ? elFadeIn(next) : alert('finished!');
 });
}​

JS Fiddle demo.

Source Link
David Thomas
  • 254.1k
  • 53
  • 382
  • 421

It's not exactly a pretty answer, but one approach is simply to assess whether there is, or is not, a next() element and re-call the function or perform a different action:

function elFadeIn(elem) {
 elem.fadeIn('slow', function() {
 if ($(this).next().length) {
 elFadeIn($(this).next());
 }
 else {
 alert('finished');
 }
 });
}​

JS Fiddle demo.

And a slightly prettier way of writing that would be:

function elFadeIn(elem, callback) {
 elem.fadeIn('slow', function() {
 var next = $(this).next();
 action = next.length ? elFadeIn(next) : alert('finished!');
 });
}​

JS Fiddle demo.

lang-js

AltStyle によって変換されたページ (->オリジナル) /