Brent
Brent

Reputation: 35

Run function only once

I'm looking to have this only happen once and not every time the third slide is equal to the current slide. How would i do that? would .once() work for this?

var currentSlide = current - 1;
var firstSlide = $('#slide1').index();
var secondSlide = $('#slide2').index();
var thirdSlide = $('#slide3').index();
if (thirdSlide == currentSlide) {
    $('img.joyceAnim').flipbook({
        'end': 4,
        'loop': false,
        'fps': 8,
        'mobileStep': 1,
        'images': 'anim/bench/%d.png'
    });
}​

Upvotes: 1

Views: 127

Answers (2)

jfriend00
jfriend00

Reputation: 707876

You can set a .data() indicator on the element when you've already animated it once and then check that indicator before animating it again:

var currentSlide = current-1; 
var firstSlide = $('#slide1').index();
var secondSlide = $('#slide2').index();
var thirdSlide = $('#slide3').index();
var flip = $('img.joyceAnim');
var alreadyFlipped = flip.data("flipped");

if(thirdSlide == currentSlide && !alreadyFlipped){
    flip.data("flipped", true).flipbook({
        'end': 4,
        'loop': false,
        'fps': 8,
        'mobileStep': 1,
        'images': 'anim/bench/%d.png'
    });   

}

Upvotes: 2

tckmn
tckmn

Reputation: 59323

var currentSlide = current-1; 
var firstSlide = $('#slide1').index();
var secondSlide = $('#slide2').index();
var thirdSlide = $('#slide3').index();
var doIt = true;

if((thirdSlide == currentSlide) && doIt){
    doIt = false;
    $('img.joyceAnim').flipbook({
        'end': 4,
        'loop': false,
        'fps': 8,
        'mobileStep': 1,
        'images': 'anim/bench/%d.png'
    });   

}

Just make a switch to see if you should do it.

Upvotes: 0

Related Questions