0

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?

nowiko
  • 2,507
  • 6
  • 38
  • 82

2 Answers2

4

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);
Community
  • 1
  • 1
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

I solve this:

window.setTimeout(function () {
     $('#ajax_message').html('');
}, 5000);
daniula
  • 6,898
  • 4
  • 32
  • 49
nowiko
  • 2,507
  • 6
  • 38
  • 82
  • 1
    use `window.setTimeout()` if you want to execute once, `setInterval` will iterate each 5 second – Girish Dec 04 '14 at 10:21