.delay() and .setTimeout()

前端 未结 5 1661
慢半拍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:10

    As far as I understand, .delay() is meant specifically for adding a delay between methods in a given jQuery queue. For example, if you wanted to fade an image into view during the span of 1 second, have it visible for 5 seconds, and then spend another second to fade it out of view again, you could do the following:

    $('#image').fadeIn(1000).delay(5000).fadeOut(1000);
    

    In this instance, .delay() is more intuitive to use since it is specifically meant for delaying events in a given jQuery queue. setImeout(), on the other hand, is a native JavaScript method that isn't intended explicitly for a queue line. If you wanted an alert box to pop up 1 second after clicking on a button, you could do the following:

    function delayAlert() {
        var x = setTimeout("alert('5 seconds later!')", 5000);
    }
    
    
    

提交回复
热议问题