Search code examples
jqueryflip

jquery - adding listeners & performance


I am playing with a jquery plugin called flip. I want to loop through a series of behaviors. I am not if this is most efficient way to do this. I suspect that I am am adding a listener with each iteration which could affect performance.

ref: http://lab.smashup.it/flip/

$(document).ready(function() {

        function myFunction(container, title,color){            
            var $this = $("#"+container);
            $this.flip({
                direction:'tb',
                content: title,
                color: color,
            })
        }

            var n=0;
            var doSomething = function(){                   
            if (n == 4) {n=1;} else {n++;}              

                switch(n)
            {
                case 1:
                myFunction("flipbox1","title1", "#1B9772");
                break;

                ...

                default:
                }
            }

            setInterval(doSomething, 1000); 

    });     

Solution

  • Don't be worried about performance, something like this shouldn't be any big deal. Callbacks are actually built into this jQuery plugin, so you need not write your own. I'd try modifying your code slightly like such:

    $(document).ready(function() {
        var title = "Hello!";
        var color = "orange";
    
        $("#myDiv").flip({
            direction:'tb',
            content: title,
            color: color,
            onBefore: function() {
                alert("Flip started");
            },
            onEnd: function() {
                alert("Flip ended");
            }
        });
    });