1

I have 135 html elements which I want to be faded in and out randomly, so I wrote this code for it:

setInterval(function() {
    ggg = Math.floor(Math.random() * (50 - 1 + 1) + 1);
    $("#f" + ggg).fadeIn(500, function() {  });
    $("#f" + ggg).fadeOut(500);
}, 300);


setInterval(function() {
    ggg = Math.floor(Math.random() * (100 - 50 + 1) + 50);
    $("#f" + ggg).fadeIn(500, function() {  });
    $("#f" + ggg).fadeOut(500); 
}, 300);

setInterval(function() {
    ggg = Math.floor(Math.random() * (135 - 100 + 1) + 100);
    $("#f" + ggg).fadeIn(500, function() {   });
    $("#f" + ggg).fadeOut(500);
}, 300);

But the problem is that it takes 50 percent of my i7 on Macbook Pro, even though there is no problem on a Windows laptop. Can someone rewrite the code for better performance?

rgettman
  • 176,041
  • 30
  • 275
  • 357
pailodze
  • 71
  • 1
  • 4

5 Answers5

0
animate = function() {
  ggg = Math.floor(Math.random() * (100 - 50 + 1) + 50);
  $("#f" + ggg).fadeIn(500, function() {
  $("#f" + ggg).fadeOut(500,animate); 
  })
}
animate();

To delay, make this modification:

  $("#f" + ggg).delay(300).fadeOut(500,animate); 

You could also use .stop(true,true) to clear the queue before starting another animation.

Casey Dwayne
  • 2,142
  • 1
  • 17
  • 32
0
$("#f" + ggg).animate({ opacity: 0 }, 500, function() {
    $("#f" + ggg).animate({ opacity: 1 }, 500)
});

Perhaps you're getting higher speed with animate, but idk. It's not uncommon for javascript to even slow down an i7 (perhaps browser related performance?)

Flame
  • 6,663
  • 3
  • 33
  • 53
0

In addition to kicking off a 1-second animation every 300ms (which makes little sense), it also appears that you're expecting the fadeIn and fadeOut functions to run synchronously.

This code:

$("#f" + ggg).fadeIn(500, function() {  });
$("#f" + ggg).fadeOut(500);

Will start a half-second fade-in, then immediately overwrite it with a half-second fade-out. I'm not even sure what that does.

That empty function you pass to fadeIn is the callback to do when it's complete... thats where you want to put the fadeOut:

$("#f" + ggg).fadeIn(500, function() {
    $("#f" + ggg).fadeOut(500);
});
bhamlin
  • 5,177
  • 1
  • 22
  • 17
0

My guess would be that the reason is the stacking of the animation calls.

You set a Interval which calls a function every 300 Milliseconds, which start 2 animations with a duration of of 500+500 Milliseconds.

Also you could do all 3 animation calls in one function. And maybe there might be a better way to select your object that you want to animate? Random might not be the most efficient way.

Maybe this might already do the trick (untested)

function animateStuff(){
    ggg = Math.floor(Math.random() * (50 -1 + 1) + 1);
    $("#f" + ggg).fadeOut(500, function(){
        $(this).fadeIn(500);
    });

    ggg = Math.floor(Math.random() * (100 -1 + 1) + 1);
    $("#f" + ggg).fadeOut(500, function(){
        $(this).fadeIn(500);
    });

    ggg = Math.floor(Math.random() * (135 -1 + 1) + 1);
    $("#f" + ggg).fadeOut(500, function(){
        $(this).fadeIn(500);
    });
}

setInterval(function(){
    animateStuff();
}, 1000);
0

Give your elements a class, cache your elements, randomize your jQuery object and then fade them in and out.

var els = $(".random");
var $random = shuffleArray( els );

$random.each( function( ) {
    var max = 4000;
    var min = 1000;
    var duration = Math.floor( Math.random() * ( max - min + 1) + min );
    $(this).delay( duration ).fadeTo( 1000, 1, function( ) {
        $(this).delay( duration ).fadeTo( 1000, 0 );
    });
});

Shuffling function found here

/**
 * Randomize array element order in-place.
 * Using Fisher-Yates shuffle algorithm.
 */
function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}

Fiddle here

Community
  • 1
  • 1
Bruno
  • 5,772
  • 1
  • 26
  • 43