From: jeresig <jeresig@gmail.com>
Date: Fri, 4 Dec 2009 17:06:47 +0000 (-0500)
Subject: Added in the .delay() method for delaying the execution of queued functions and anima... 
X-Git-Url: http://git.asbjorn.biz/?a=commitdiff_plain;h=bbd933cbfe6d31a749cb336d7a84155ccfab247f;p=jquery.git

Added in the .delay() method for delaying the execution of queued functions and animations.
---

diff --git a/src/data.js b/src/data.js
index 079cf99..3e6eb19 100644
--- a/src/data.js
+++ b/src/data.js
@@ -166,6 +166,21 @@ jQuery.fn.extend({
 			jQuery.dequeue( this, type );
 		});
 	},
+
+	// Based off of the plugin by Clint Helfers, with permission.
+	// http://blindsignals.com/index.php/2009/07/jquery-delay/
+	delay: function( time, type ) {
+		time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
+		type = type || "fx";
+
+		return this.queue( type, function() {
+			var elem = this;
+			setTimeout(function() {
+				jQuery.dequeue( elem, type );
+			}, time );
+		});
+	},
+
 	clearQueue: function(type){
 		return this.queue( type || "fx", [] );
 	}
diff --git a/test/unit/data.js b/test/unit/data.js
index d3241c9..d18377d 100644
--- a/test/unit/data.js
+++ b/test/unit/data.js
@@ -257,6 +257,21 @@ test("queue() passes in the next item in the queue as a parameter to fx queues",
 
 });
 
+test("delay()", function() {
+	expect(2);
+	stop();
+
+	var foo = jQuery({}), run = 0;
+
+	foo.delay(100).queue(function(){
+		run = 1;
+		ok( true, "The function was dequeued." );
+		start();
+	});
+
+	equals( run, 0, "The delay delayed the next function from running." );
+});
+
 test("clearQueue(name) clears the queue", function() {
 	expect(1);