Skip to content
Browse files

Added code examples

  • Loading branch information...
1 parent 922ba37 commit a63eb563474e19b53d72cc743c8737f6547d771c @MoonScript committed Feb 28, 2013
Showing with 36 additions and 2 deletions.
  1. +36 −2 README.md
View
38 README.md
@@ -1,4 +1,38 @@
-jquery.wait
+jquery-wait
===========
-$.wait jQuery extension that wraps setTimeout in a $.Deferred object
+$.wait jQuery extension that wraps setTimeout in a [Deferred](http://api.jquery.com/category/deferred-object/) object.
+
+Basic example:
+
+```JavaScript
+$.wait(500).then(function(){
+ console.log('A half-second later.');
+});
+```
+
+This is similar to [_.delay](http://underscorejs.org/#delay), except that you can cancel the *done* callbacks by calling `.reject()` on the returned Deferred object:
+
+```JavaScript
+var deferred = $.wait(500).done(function(){
+ console.log('This won\'t run if the deferred object is rejected.');
+});
+
+if (confirm('Should we cancel the queued action?')) {
+ deferred.reject();
+}
+```
+
+Use it along side the Promise-enhanced *jqXHR* object returned from [$.ajax](http://api.jquery.com/jQuery.ajax/):
+
+```JavaScript
+var jqXHR = $.ajax({
+ // ajax options here
+});
+
+$.when(jqXHR, $.wait(500)).done(function(){
+ console.log('This won\'t run until the AJAX has returned and at least 500 ms have passed.');
+});
+```
+
+> Use [$.when](http://api.jquery.com/jQuery.when/) to know when all Deferred objects have been completed.

0 comments on commit a63eb56

Please sign in to comment.
Something went wrong with that request. Please try again.