然而,在JavaScript中,我该如何知道对象何时被销毁?下面的示例是我关心的情况。
我正在超时内部调用一个函数,并且想知道当计时器运行并等待再次调用时,对象是否仍然存在。 用户单击调用控件
// Calls Control
控件调用消息
var message_object = new Message( response_element );
消息调用效果
new Effects().fade( this.element, 'down', 4000 );
message_object.display( 'empty' );
效果
/**
*Effects - build out as needed
* element - holds the element to fade
* direction - determines which way to fade the element
* max_time - length of the fade
*/
var Effects = function( )
{
this.fade = function( element, direction, max_time )
{
element.elapsed = 0;
clearTimeout( element.timeout_id );
function next()
{
element.elapsed += 10;
if ( direction === 'up' )
{
element.style.opacity = element.elapsed / max_time;
}
else if ( direction === 'down' )
{
element.style.opacity = ( max_time - element.elapsed ) / max_time;
}
if ( element.elapsed <= max_time )
{
element.timeout_id = setTimeout( next, 10 );
}
}
next();
}
};