Search code examples
javascriptjquerydelay

Remove content form tag after 5 seconds


after I get data and set them into tag, after 5 seconds I want to purge this tag. I try:

$('#ajax_message').html(data).delay(5000).html("");

But this doesnt work. How I can do that?


Solution

  • As per doc:

    jQuery.delay() is best for delaying between queued jQuery effects and such, and is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

    long story short, .delay() works for queued functions. You should use setTimeout() instead.

    $('#ajax_message').html(data)
    setTimeout(function(){
      $('#ajax_message').html("");
    },5000);