How to delay an alert?

I try to postpone an alert, but I can’t get it to work, it always appears when #foo freezes and increases in size:

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

but I want it to show only after #foo has returned to its original state ...

+4
source share
4 answers

How to use callback

 $('#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'); }); }); 
+6
source

You need to do this in the callback. This is the function that will be called when the animation is complete:

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

See the animate API .

+2
source

Does it look like you want to use a callback for an animation function?

http://api.jquery.com/animate/

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

Use the setTimeout function: http://www.w3schools.com/js/js_timing.asp

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

The above code should be delayed for 3 seconds.

-1
source

Source: https://habr.com/ru/post/1333209/


All Articles