Search code examples
javascriptjquerysliderflickity

How do I get this If statement to run once until it comes back round (the condition is constantly true until slide changes)


I've created a Flickity slider through Jquery and require a little assitance. I'm using the selectedIndex as a condition to check what slider is visable. See code below:

    $carousel.on( 'select.flickity', function() {
    if (flkty.selectedIndex === 0) {
        hideAllText();
        text0.fadeIn('slow');
    }
});

This is saying, when slider 0 (initial slide) fade in var text0. I want to do this for 6 sliders but as the slide doesn't change until 4 seconds has been the if statement keeps running so it keeps hiding and showing text.

Any ideas how I can make this a one time run until the slide comes round again.

Thanks :)


Solution

  • This should result in the relevant code only being executed once:

    text0Shown = false;
    
    $carousel.on('select.flickity', function() {
      if (!text0Shown && flkty.selectedIndex === 0) {
        text0Shown = true;
        hideAllText();
        text0.fadeIn('slow');
      }
    });