.delay() and .setTimeout()

前端 未结 5 1768
慢半拍i
慢半拍i 2020-11-28 10:29

According to jQuery document on .delay(),

The .delay() method is best for delaying between queued jQuery effects. Because it is limited

5条回答
  •  自闭症患者
    2020-11-28 11:18

    You can use delay with animations, for example:

    $('.message').delay(5000).fadeOut();
    

    You can also use timeOut to delay the start of animations, for example:

    window.setTimeout(function(){
      $('.message').fadeOut();
    }, 5000);
    

    As you see, it's easier to use delay than setTimeout with animations.

    You can delay pretty much anything with setTimeout, but you can only delay animations with delay. Methods that aren't animations are not affected by delay, so this would not wait a while before hiding the element, it would hide it immediately:

    $('.message').delay(5000).hide();
    

提交回复
热议问题