aizaz
aizaz

Reputation: 3062

setTimeout method :displaying alerts in two different time intervals

I am trying to display the two alert box in two different time intervals

<script>
function myFunction()
{
       setTimeout(function(){alert("Hello")},3000);    
       setTimeout(function(){alert("Hello World")},3000);
}
</script>

I want to display Hello alert after 3 sec and Hello World after 3 sec of 1st alert, but 2nd alert displaying immediate to 1st one.

How to make it work?

Thank You

Upvotes: 1

Views: 1186

Answers (2)

mohkhan
mohkhan

Reputation: 12325

Put the second setTimeout in the first one. Like this...

setTimeout(function(){
    alert("Hello");
    setTimeout(function(){alert("Hello World")},3000);
},3000); 

Upvotes: 1

jerrylow
jerrylow

Reputation: 2629

You can nest a timeout in the function of the first one:

setTimeout(function(){
    alert("Hello");    
    setTimeout(function(){alert("Hello World")},3000);
},3000);

Upvotes: 5

Related Questions