JQuery delay() not delaying

后端 未结 3 1445
生来不讨喜
生来不讨喜 2020-12-01 18:26

Why does this empty the text immediately (ignoring delay)?

$(\'#error_box_text\').html(\'error text\').delay(5000).html(\'\')

#

j

相关标签:
3条回答
  • 2020-12-01 18:43

    Try

    var sel = $('#error_box_text');
    sel.html('error text');
    setTimeout(function(){
        sel.html('');
    }, 5000);
    

    See delay()

    jQuery.delay() is best for delaying between queued jQuery effects and such, and is not a replacement for JavaScript's native setTimeout function

    0 讨论(0)
  • 2020-12-01 18:56

    Just use something like this:

    let r = $("#ur-div-id")
    r.html(d)
    setTimeout(() =>r.empty(), 4850);
    
    0 讨论(0)
  • 2020-12-01 19:00

    delay will never delay regular methods - only those who are pushed to the animation/effect chain. If you want to delay your html() call, use queue ( http://api.jquery.com/queue/ ):

    $('#error_box_text').html('error text').delay(5000).queue(function() {
       $(this).html('')
    });
    

    It would be nice if you could do

    $('#error_box_text').html('error text').delay(5000, function() { $(this).html('') });
    

    but this isn't possible (yet).

    0 讨论(0)
提交回复
热议问题