0

This is a bit awkward but I need an array of static variables. Each time a function is called I need access to these variables. Basically I have an effects functions that is "controlled" by a single variable currently called elapsed_time.

However, I need an elapsed_time for each element that is passed to the function so I can make sure not to run effects on the same element...(for my fade function this causes a flickering effect).

I would like to use the element.id to name my variables.

I just read a post saying that associative arrays are not really associative arrays and that you should not use them here.

Below is my current function that I need to update.

/**
 *    Effects
 */

var Effects = function( element ) 
{
    this.element = element;
};

Effects.prototype.fade = function( direction, max_time ) 
{
    Effects.elapsed = 0;
/*
    Effects.arrayHold = [];
    Effects.arrayHold.push( this.element.id );
*/
    var persist_element = this.element;
    function next() 
    {
        Effects.elapsed += 10;
        if ( direction === 'up' )
        {
            persist_element.style.opacity = Effects.elapsed / max_time;
        }
        else if ( direction === 'down' )
        {
            persist_element.style.opacity = ( max_time - Effects.elapsed ) / max_time;
        }
        if ( Effects.elapsed <= max_time ) 
        {
            setTimeout( next, 10 );
        }
    }
    next();
    return true;
};
3
  • Seriously, look into the fadeIn() and fadeOut() functions built into jQuery; there is also a fadeToggle() function; all with duration, easing, and callback functional options. Did I mention the cross-browser capabilities? Commented Apr 7, 2012 at 16:41
  • What do you care about some blog post? Use an associative array when its the best tool and just avoid the pitfalls. Commented Apr 7, 2012 at 16:43
  • Don't declare Effects,arrayHold as an array, because you're using it as an object. so better use Effects.objectHold = {persust_element.id : 0}; and take it from there Commented Apr 7, 2012 at 17:27

1 Answer 1

1

Just use an Object instead of an array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.