0

The code I have flips 6 tiles after the page is loaded. This works fine.

var flipTiles = function() {
    $('.flip-tile').each(function( i, el ) {
        var elDelay = $(el).data('delay');
        $(el).delay(elDelay).queue(function() {
            $(this).flip(true);
            $(this).dequeue();
        });
    });
};

flipTiles();

My question is how could I make this so the tiles keep flipping. Like there page loads and the tiles flip with there own delay. After all tiles flipped I wanna restart the sequence and flip them back etc.

Any help is appreciated! Thanks in advance :)

2 Answers 2

1

You'll need to unwrap the loop and make each step trigger the next in sequence, see comments:

var flipTiles = function() {
    // Grab the tiles
    var tiles = $('.flip-tile');
    // Start with the first tile
    var i = 0;
    // Do that tile
    next();
    function next() {
        var tile = tiles.eq(i);
        tile.delay(tile.data('delay')).queue(function() {
            tile.flip(true);
            tile.dequeue();
            // Increment `i`, wrap-around at the end
            i = (i + 1) % tiles.length;
            // Do the next tile
            next();
        });
    }
};

flipTiles();

If you want to make it possible to cancel it, return something the caller can use to do that:

var flipTiles = function() {
    var running = true;
    // Grab the tiles
    var tiles = $('.flip-tile');
    // Start with the first tile
    var i = 0;
    // Do that tile
    next();
    // Return something the caller can use to cancel
    return function() {
        running = false;
    };
    function next() {
        if (!running) {
            return;
        }
        var tile = tiles.eq(i);
        tile.delay(tile.data('delay')).queue(function() {
            tile.flip(true);
            tile.dequeue();
            // Increment `i`, wrap-around at the end
            i = (i + 1) % tiles.length;
            // Do the next tile
            next();
        });
    }
};

var cancel = flipTiles();
// (later)
cancel();
Sign up to request clarification or add additional context in comments.

4 Comments

This will not work, because you will enter the function next and never leave it.
@DaniloKörber- No, you won't. Try it! next returns almost immediately, having scheduled some asynchronous callbacks, and then flipTiles returns almost immediately. The tiles keep flipping as the asynchronous callbacks get called by the browser's timer mechanism.
@t-j-crowder - I tried it and the code never leaves the function next... jsfiddle.net/dkorber/52uabqn9/6
@DaniloKörber - The code in that fiddle is missing the critical aspect I called out in my comment above: Scheduling asynchronous callbacks. You've just completely discarded the relevant bit. I suggest you review delay and queue.
0

To "loop forever" you can use the setTimeout event:

myVar = setTimeout(function() { flipTiles(); }, 3000); // 3000 is the value in milliseconds to repeat the command

to stop the "loop" use

clearTimeout(myVar);

2 Comments

This won't work for two reasons. Firstly because you're providing the response of the flipTiles() function to setTimeout(), not the function reference. Secondly because you're telling it to flip 6 cards every 3 seconds, not flip them infinitely at a specific interval
Thanks, @rory-mcgrossan. I corrected the code that calls the function. The three seconds are only an example, he has to adjust it to his needs.

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.