0

Possible Duplicate:
Javascript sleep
How to delay execution in between the following in my javascript

Hi i want to change html of two divs. But have pause between it

$(#div1).html('Div1 content');
$(#div2).html('Div2 content');
//wait 5 seconds
$(#div1).html('Div1 new content');
//wait 5 seconds
$(#div2).html('Div2 new content');
Community
  • 1
  • 1
dr0zd
  • 1,368
  • 4
  • 18
  • 28
  • 1
    possible duplicate of [Javascript sleep](http://stackoverflow.com/questions/951021/javascript-sleep) and (closer related) [How to delay execution in between the following in my javascript](http://stackoverflow.com/questions/5990725/how-to-delay-execution-in-between-the-following-in-my-javascript) – Felix Kling May 09 '12 at 12:44

3 Answers3

3

A quick way to do it would be to chain a couple setTimeout()s.

$('#div1').html('Div1 content');
$('#div2').html('Div2 content');

setTimeout(function () {
    $('#div1').html('Div1 new content');

    setTimeout(function () {
        $('#div2').html('Div2 new content');
    }, 5000);

}, 5000);

Don't forget to quote your $() selectors, too.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
2

You can use the setTimeOut() function

romainberger
  • 4,563
  • 2
  • 35
  • 51
0
$(document).ready(function() {
$('#div1').html('Div1 content');
$('#div2').html('Div2 content');

//wait 5 seconds   
$('#div1').delay(5000)
    .queue(function(n) {
        $(this).html("Div1 new content");       
    });

//wait another 5 seconds
$('#div2').delay(10000)
    .queue(function(n) {
        $(this).html("Div2 new content");       
    });
});
arun
  • 3,667
  • 3
  • 29
  • 54