views:

552

answers:

2

Hi

I am wondering how can I hide/remove a tag after a certain amount of time. Is there some built in thing or do I do use threading(if javascript can do this?)

+13  A: 

You don't even need jQuery for the "5 seconds" part: JavaScript's built-in setTimeout function will do the trick. Incorporating jQuery for the DOM manipulation, you get:

setTimeout(function() {
  $("#the-tag-you-want-to-remove").remove();
}, 5000);

Here the 5000 represents 5000 milliseconds, or 5 seconds. You can pass setTimeout an existing function or (as in this case) an anonymous function.

VoteyDisciple
another thing to note is that you can pass it a string to evaluate, though i would discourage against this unless absolutely necessary
Darko Z
eeek when is it necessary?
redsquare
Also I always use the "window" prefix because you never know when somebody else (plugin etc) will create a func/var with the same name in scope that does something else
redsquare
+2  A: 
window.setTimeout( hideTagFn, 5000);

function hideTagFn(){

   $('#someElementId').hide();
}
redsquare