0

I have a simple function that I wrote that transitions three div elements using a fade in/out effect. The event is triggered when a user clicks a link. Here's my code:

$(".link1").click(function () {
   $(".feature1").fadeIn(1000);
   $(".feature2").fadeOut(1000);
   $(".feature3").fadeOut(1000);
});

$(".link2").click(function () {
   $(".feature1").fadeOut(1000);
   $(".feature2").fadeIn(1000);
   $(".feature3").fadeOut(1000);
});

$(".link3").click(function () {
   $(".feature1").fadeOut(1000);
   $(".feature2").fadeOut(1000);
   $(".feature3").fadeIn(1000);
});

I need to be able to set some sort of timer so that these transitions happen automatically every 8 seconds or so. I also want them to "loop" essentially, so that if we get to the third div in the set, it returns to the first div.

1
  • have you tried setInterval()? Commented Jul 26, 2012 at 20:42

4 Answers 4

1

Try using setTimeout() function.

var timer = null;

function foo_loop(div, timeout) {
  if (div > 3) div = 1;
  $(".feature"+div).fadeIn(1000);
  $("div[class^=feature]:not(.feature"+div+")").fadeOut(1000);
  clearTimeout(timer);
  timer = setTimeout(function() {
    foo_loop(div + 1, timeout);
  }, timeout);
}

Run this like that (To start with first div and 8 second timeout):

foo_loop(1, 8000);

Function for stopping loop:

function stop_loop() {
  clearTimeout(timer);
}

Run it when you need to stop the loop (for example on click on element with id="stop"):

$('#stop').bind('click', stop_loop);
Sign up to request clarification or add additional context in comments.

6 Comments

This is perfect, and exactly what I was looking for. Is there any way that I can pause the automatic transition when I click one of the links included in my original example?
Yes you can - create global variable var timer = null and change function: clearTimeout(timer); timer = setTimeout(function() { foo_loop(div + 1, timeout); }, timeout); And add stop event: $('#stop').bind('click', function() { clearTimeout(timer); });
Do I add this to the original code, or is it a separate function? Do you mind updating your original answer to reflect how this would be added?
Thanks, this mostly appears to work. However, it doesn't work when the item clicked is the final div in the set. The loop keeps on running. Is that because this rule overrides the stop_loop function? if (div > 3) div = 1;
Extend stop_loop() function with alert to be sure it is called. It should clear timer so that loop stops.
|
1

setInterval(expression, timeout); runs the function in intervals, with the length of the timeout between them

example:

var intervalID = setInterval(alert('heelo'), 3000); // will alert hello every 3 seconds

// clearInterval(intervalID); // will clear the timer

1 Comment

this won't work, you need to pass a function(reference) or an expression which is essentially a string and not recommendable.
1

Try the following:

var i = 0;
var transition = setInterval(function(){
                     i++;
                     if (i == 4) {i = 1}
                     $(".feature"+i).stop().fadeIn(1000, function(){
                        $(this).delay('6000').fadeOut(1000)
                     })
                  }, 8000)

4 Comments

or maybe easier: instead of $(".feature"+i).stop().fadeIn(1000, function(){ $(this).delay('7000').fadeOut(1000) }) just go with $(".link"+i).click()
@Horen triggering .click() is mostly not recommendable. Rather put it into a function and call the function instead.
@Christoph isn't click() a function? why is it not recommendable?
@Horen I meant it in another way - mostly you pass an anonymous function to the eventhandler. Just use a named function and pass the function-reference to the eventhandler. This way you have the option call the named function without triggering the event. Triggering events programmatically is not recommendable because you dont have all parameters of the event object this way.
0

not sure I fully understand when you want them to loop or how often, but I think this should help you out a bit... every 8 seconds it loops through the animations...

function fadeLoop(selectors, animations, times, index) {
    index = index || 0;
    if(index == selectors.length) return;
    $((selectors[index])[animations[index]](times[index], function() {
        fadeLoop(selectors, animations, times, index + 1);
    });
}

setInterval(function() {
    fadeLoop(
        ['.feature1', '.feature2', '.feature3'],
        ['fadeIn', 'fadeOut', 'fadeOut'],
        [1000, 1000, 1000]
    );

    fadeLoop(
        ['.feature1', '.feature2', '.feature3'],
        ['fadeOut', 'fadeIn', 'fadeOut'],
        [1000, 1000, 1000]
    );

    fadeLoop(
        ['.feature1', '.feature2', '.feature3'],
        ['fadeOut', 'fadeOut', 'fadeIn'],
        [1000, 1000, 1000]
    );
}, 1000 * 8);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.