w3resource

jQuery Effect: Set a timer to delay execution of subsequent items in the queue

jQuery Effect : Exercise-3 with Solution

Set a timer to delay execution of subsequent items in the queue.

Sample Data :
HTML code:

<body>
<button id="btn1">Start Animate</button>
<button id="btn2">Stop Animate</button>
<div id="box" style="background:#5858FA;height:100px;width:100px;margin:6px;"></div>  
</body>

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Set a timer to delay execution of subsequent items in the queue</title>
</head>
<body>
<p>delay() method sets different speed values.</p>
<button>Click to fade in boxes with a delay</button></p>
<div id="div1" style="width:100px;height:90px;display:none;background-color:#9F81F7;"></div><br>
<div id="div2" style="width:100px;height:90px;display:none;background-color:#0B610B;"></div><br>
<div id="div3" style="width:100px;height:90px;display:none;background-color:#61210B;"></div><br>
</div>
</body>
</html>

JavaScript Code :

 $("button").click(function(){
    $("#div1").delay(800).fadeIn();
    $("#div2").delay(2400).fadeIn();
    $("#div3").delay(4000).fadeIn();
  });

Note:
delay( duration [, queueName ] ) method is used to set a timer to delay execution of subsequent items in the queue. It has the following parameters :

  • duration : An integer represents the number of milliseconds to delay execution of the next item in the queue. [Type: Integer]
  • queueName : A string containing the name of the queue (Default -> fx, the standard effects queue). [Type: String]

Live Demo:

See the Pen jquery-effect-exercise-3 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Stop an Animation.
Next: Use dequeue to end a custom queue function which allows the queue to keep going.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.