HighDots Forums  

Iterating over a list

jQuery jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript.


Discuss Iterating over a list in the jQuery forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
gthorne
 
Posts: n/a

Default Iterating over a list - 11-07-2009 , 07:45 PM






Hi, real simple problem, but I am struggling to find a solution. I
have an array:

var arr = [ "one", "two", "three", "four", "five" ];

and I want to fade the values in and out, moving on to the next one.
I've tried 'jQuery.each()', for loops, and so on, but I either get the
last one of the list fading in and out over and over, or it doesn't
work at all.

What I tried first was:

$(document).ready(function(){
var arr = [ "one", "two", "three", "four", "five" ];

jQuery.each (arr, function() {
$('#box_content').text(this).fadeIn().fadeOut();
});
});

Now, I'm up to this:

$(document).ready(function(){
var arr = [ "one", "two", "three", "four", "five" ];

for (i=0; i<5; i++)
{
$("#box_content").fadeOut('slow').fadeIn('slow', function () { $
(this).text(arr[i]); });
}
});

I think what's happening on a lot of attempts is that the loop is
going on through, and not waiting for the function. So, I was hoping
that putting it in the callback would solve that.

I've done a lot of ajax with jquery, but evidently, I need to brush up
on my animation stuff.

Thanks for your help.
-Greg

Reply With Quote
  #2  
Old   
Michel Belleville
 
Posts: n/a

Default Re: [jQuery] Iterating over a list - 11-08-2009 , 02:27 AM






When you start an animation like fadeIn() the animation starts in the
background and the script goes on without waiting the animation ton
complete, so with your each you're changing the texts one by one blazingly
fast even before the first fadeIn() completes.

What you might want instead is something like this :

var recursive_anim = function(list, step, target, duration) {
if (list[step]) {
target(text).fadeIn(duration, function() {
$(this).fadeOu(duration, function() {
recursive_anim(list, step + 1, $(this), duration)
});
});
}
};

fadeIn() and fadeOut() callbacks are triggered as the animation completes.

Michel Belleville


2009/11/8 gthorne <gthorne (AT) gmail (DOT) com>

Quote:
Hi, real simple problem, but I am struggling to find a solution. I
have an array:

var arr = [ "one", "two", "three", "four", "five" ];

and I want to fade the values in and out, moving on to the next one.
I've tried 'jQuery.each()', for loops, and so on, but I either get the
last one of the list fading in and out over and over, or it doesn't
work at all.

What I tried first was:

$(document).ready(function(){
var arr = [ "one", "two", "three", "four", "five" ];

jQuery.each (arr, function() {
$('#box_content').text(this).fadeIn().fadeOut();
});
});

Now, I'm up to this:

$(document).ready(function(){
var arr = [ "one", "two", "three", "four", "five" ];

for (i=0; i<5; i++)
{

$("#box_content").fadeOut('slow').fadeIn('slow', function () { $
(this).text(arr[i]); });
}
});

I think what's happening on a lot of attempts is that the loop is
going on through, and not waiting for the function. So, I was hoping
that putting it in the callback would solve that.

I've done a lot of ajax with jquery, but evidently, I need to brush up
on my animation stuff.

Thanks for your help.
-Greg

Reply With Quote
  #3  
Old   
Sam Doyle
 
Posts: n/a

Default Re: [jQuery] Iterating over a list - 11-08-2009 , 05:05 AM



$(this).fadeOu(duration, function() {


Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville
<michel.belleville (AT) gmail (DOT) com> wrote:

> $(this).fadeOu(duration, function() {

Reply With Quote
  #4  
Old   
Michel Belleville
 
Posts: n/a

Default Re: [jQuery] Iterating over a list - 11-08-2009 , 02:16 PM



Typo indeed ^^°

Though the principle is sound.

Michel Belleville


2009/11/8 Sam Doyle <sammeh.mp3 (AT) gmail (DOT) com>

Quote:
$(this).fadeOu(duration, function() {


Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville <michel.belleville (AT) gmail (DOT) com
wrote:

$(this).fadeOu(duration, function() {


Reply With Quote
  #5  
Old   
gthorne
 
Posts: n/a

Default Re: Iterating over a list - 11-08-2009 , 08:36 PM



I'm not sure I follow what the principle is?


On Nov 8, 1:16*pm, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com>
wrote:
Quote:
Typo indeed ^^°

Though the principle is sound.

Michel Belleville

2009/11/8 Sam Doyle <sammeh.... (AT) gmail (DOT) com

$(this).fadeOu(duration, function() {

Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com
wrote:

$(this).fadeOu(duration, function() {

Reply With Quote
  #6  
Old   
Michel Belleville
 
Posts: n/a

Default Re: [jQuery] Re: Iterating over a list - 11-09-2009 , 01:38 AM



To use callbacks that are triggered actually when the animation finishes
instead of calling the animations all at once and seeing only the very last
because they all start approximately at the same time.

Michel Belleville


2009/11/9 gthorne <gthorne (AT) gmail (DOT) com>

Quote:
I'm not sure I follow what the principle is?


On Nov 8, 1:16 pm, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com
wrote:
Typo indeed ^^°

Though the principle is sound.

Michel Belleville

2009/11/8 Sam Doyle <sammeh.... (AT) gmail (DOT) com

$(this).fadeOu(duration, function() {

Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville
michel.bellevi... (AT) gmail (DOT) com
wrote:

$(this).fadeOu(duration, function() {

Reply With Quote
  #7  
Old   
gthorne
 
Posts: n/a

Default Re: Iterating over a list - 11-09-2009 , 11:36 AM



Is that close to what I was doing on the second example, where I was
calling $().text in the callback portion of fadeIn()?

On Nov 9, 12:38*am, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com>
wrote:
Quote:
To use callbacks that are triggered actually when the animation finishes
instead of calling the animations all at once and seeing only the very last
because they all start approximately at the same time.

Michel Belleville

2009/11/9 gthorne <gtho... (AT) gmail (DOT) com

I'm not sure I follow what the principle is?

On Nov 8, 1:16 pm, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com
wrote:
Typo indeed ^^°

Though the principle is sound.

Michel Belleville

2009/11/8 Sam Doyle <sammeh.... (AT) gmail (DOT) com

$(this).fadeOu(duration, function() {

Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville
michel.bellevi... (AT) gmail (DOT) com
wrote:

$(this).fadeOu(duration, function() {

Reply With Quote
  #8  
Old   
Michel Belleville
 
Posts: n/a

Default Re: [jQuery] Re: Iterating over a list - 11-09-2009 , 12:30 PM



Your second example was closer, considering you triggered the text change in
a callback, but you still triggered the animations all at once which I think
wasn't the effect you desired.

So how does the example I've given you work ? Let's comment :

var recursive_anim = function(list, step, target, duration) {
// this is a recursive function that uses :
// - list which is a list of texts to set in the target between each
animation
// - step the position in the list of the text to set in the target before
this cycle of animation starts (0 => first text, 1 => second text, ...)
// - target the element to animate
// - duration the duration of the fadeIn() and the fadeOut()

if (list[step]) { // when we're through the list, we stop
target.text(text).fadeIn(duration, function() { // we change the target's
text and we start the fadeIn()
$(this).fadeOut(duration, function() { // the callback is triggered once the
fadeIn() finishes, and we're using our target (the element that juste faded
in) to start the fadeOut()
recursive_anim(list, step + 1, $(this), duration) // the fadeOut callback is
triggered once the fadeOut() also finishes, and we're trying the next step
of animation (taking the next text and doing everything over with this text)
});
});
}
};

I corrected a small but significant typo on the line
target.text(text).fadeIn(...), obviously I had forgotten the .text() method
in my earlier example.

Hope it helps.

Michel Belleville


2009/11/9 gthorne <gthorne (AT) gmail (DOT) com>

Quote:
Is that close to what I was doing on the second example, where I was
calling $().text in the callback portion of fadeIn()?

On Nov 9, 12:38 am, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com
wrote:
To use callbacks that are triggered actually when the animation finishes
instead of calling the animations all at once and seeing only the very
last
because they all start approximately at the same time.

Michel Belleville

2009/11/9 gthorne <gtho... (AT) gmail (DOT) com

I'm not sure I follow what the principle is?

On Nov 8, 1:16 pm, Michel Belleville <michel.bellevi... (AT) gmail (DOT) com
wrote:
Typo indeed ^^°

Though the principle is sound.

Michel Belleville

2009/11/8 Sam Doyle <sammeh.... (AT) gmail (DOT) com

$(this).fadeOu(duration, function() {

Typo

Sent from my iPhone

On 8 Nov 2009, at 07:27, Michel Belleville
michel.bellevi... (AT) gmail (DOT) com
wrote:

$(this).fadeOu(duration, function() {

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.