2010-12-23 38 views
4

我试图拖延警报,但不能得到它的工作,它总是弹出时#foo悬停和规模在不断扩大:如何延迟警报?

$('#foo').hover(function() { 
$(this).animate({width :'400px'}, 'slow'); 
}, 
function() { 
$(this).delay(2000).animate({width :'40px'}, 'slow'); 
alert('Im an alert message'); 
}); 

,但我想让它显示后,才#foo已经降回到原来的状态......

回答

-1

使用setTimeout函数:http://www.w3schools.com/js/js_timing.asp

$('#foo').hover(function() { 
setTimeout(function(){$(this).animate({width :'400px'}, 'slow');}, 3000); 
} 

上面的代码应该持续3秒延时。

+0

这有什么好做延迟警报 – Ljubisa 2014-06-08 01:53:11

6

如何使用回调?

$('#foo').hover(function() { 
    $(this).animate({ 
     width: '400px' 
    }, 'slow'); 
}, function() { 
    $(this).delay(2000).animate({ 
     width: '40px' 
    }, 'slow', function() { //callback function, which runs very next to .animate() 
     alert('Im an alert message'); 
    }); 
}); 
+0

THX所有回信,上面的代码工作:) – Gemma 2010-12-23 20:46:58

2

您需要在回调中执行此操作。这是一个将被调用时,在动画完成功能:

$(this).delay(2000).animate({width :'40px'}, 'slow', function(){ 
    alert("I'm an alert message"); 
}); 

animate API

1

它看起来像你想在动画功能上使用回调?

$(this).delay(2000).animate({width :'40px'}, 
    'slow', 
    function() { 
     alert('Im an alert message'); 
    } 
);