我的问题是如何让一个函数的多个实例运行。下面是我的功能 - 一个简单的淡入淡出功能。我遇到的问题是,当它第二次被调用时,它会放弃第一次调用。因此,如果用户单击一个按钮,它将显示一条消退的消息。
如果用户单击另一个按钮,则先前的淡入淡出消息将停止在当前的不透明度级别。
?
什么是停止机制?之前的功能哪里去了?
功能
function newEffects(element, direction, max_time )
{
newEffects.arrayHold = [];
newEffects.arrayHold[element.id] = 0;
function next()
{
newEffects.arrayHold[element.id] += 10;
if ( direction === 'up' )
{
element.style.opacity = newEffects.arrayHold[element.id] / max_time;
}
else if ( direction === 'down' )
{
element.style.opacity = ( max_time - newEffects.arrayHold[element.id] ) / max_time;
}
if ( newEffects.arrayHold[element.id] <= max_time )
{
setTimeout( next, 10 );
}
}
next();
return true;
};
召唤
newEffects(this.element, 'down', 4000 );
旧对象
/**
* Effects - causes all elements that are fading to sync. to same opacity level as they share the same elapsed time.
*/
var Effects = function( element )
{
this.element = element;
};
Effects.prototype.fade = function( direction, max_time )
{
Effects.elapsed = 0;
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;
};