17

Is it possible, using jQuery, to fire off an event to set a div tag's text after n. seconds?

Thanks! George

George Johnston
  • 31,652
  • 27
  • 127
  • 172

3 Answers3

28
var doIt = function() {
    $("div.my").text("My message");
}
setTimeout(doIt, 3000);
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
15

if you're using jQuery 1.4, you can always do:

$(function() {
   $('#divId').delay(3000).text('New Text');
});

Ed James
  • 10,385
  • 16
  • 71
  • 103
4

I've been using the following jQuery plugins below for this. Delay can be used with chained functions, notNow can't.

Delay

http://plugins.jquery.com/project/delay

$('#animate-this').fadeIn().delay(500).fadeOut();

notNow

http://plugins.jquery.com/project/notNow

$.notNow(2000, function() { 
    alert('woolsworth');
});
John Himmelman
  • 21,504
  • 22
  • 65
  • 80
  • 1
    +1 I believe that the Delay plugin you mentioned is actually now part of the main jQuery library, as per my answer. – Ed James Feb 05 '10 at 16:42