Permalink
Browse files

Added callback-as-string signature, where callback can be a string me…

…thod name of $ or $.fn (contextually) per issue #1. Also callback now requires true, not "truthy" value to start polling.
  • Loading branch information...
1 parent 6fdbd47 commit c6d9eb93a9e90bc221ba2c09d7594bb28dc07705 @cowboy committed Mar 3, 2010
View
@@ -1,7 +1,7 @@
# jQuery doTimeout: Like setTimeout, but better! #
[http://benalman.com/projects/jquery-dotimeout-plugin/](http://benalman.com/projects/jquery-dotimeout-plugin/)
-Version: 0.4, Last updated: 7/15/2009
+Version: 1.0, Last updated: 3/3/2010
jQuery doTimeout takes the work out of delayed code execution, including interval and timeout management, polling loops and debouncing. In addition, it's fully jQuery chainable, with a very simple, yet powerful API.
@@ -27,22 +27,23 @@ tested with, what browsers it has been tested in, and where the unit tests
reside (so you can test it yourself).
### jQuery Versions ###
-1.3.2, 1.4.1
+1.3.2, 1.4.2
### Browsers Tested ###
-Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
+Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome 4-5, Opera 9.6-10.1.
### Unit Tests ###
[http://benalman.com/code/projects/jquery-dotimeout/unit/](http://benalman.com/code/projects/jquery-dotimeout/unit/)
## Release History ##
+1.0 - (3/3/2010) Callback can now be a string, in which case it will call the appropriate `$.method` or `$.fn.method`, depending on where `.doTimeout` was called. Callback must now return `true` (not just a truthy value) to poll.
0.4 - (7/15/2009) Made the "id" argument optional, some other minor tweaks
0.3 - (6/25/2009) Initial release
## License ##
-Copyright (c) 2009 "Cowboy" Ben Alman
+Copyright (c) 2010 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL licenses.
[http://benalman.com/about/license/](http://benalman.com/about/license/)

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -13,7 +13,7 @@
<div id=Index><div class=IPageTitle>File Index</div><div class=INavigationBar>$#! &middot; 0-9 &middot; A &middot; B &middot; C &middot; D &middot; E &middot; F &middot; G &middot; H &middot; I &middot; <a href="#J">J</a> &middot; K &middot; L &middot; M &middot; N &middot; O &middot; P &middot; Q &middot; R &middot; S &middot; T &middot; U &middot; V &middot; W &middot; X &middot; Y &middot; Z</div><table border=0 cellspacing=0 cellpadding=0><tr><td class=IHeading id=IFirstHeading><a name="J"></a>J</td><td></td></tr><tr><td class=ISymbolPrefix id=IOnlySymbolPrefix>&nbsp;</td><td class=IEntry><a href="../files/jquery-ba-dotimeout-js.html#jQuery_doTimeout:Like_setTimeout,but_better!" id=link1 onMouseOver="ShowTip(event, 'tt1', 'link1')" onMouseOut="HideTip('tt1')" class=ISymbol>jQuery doTimeout:<wbr>Like setTimeout,but better!</a></td></tr></table>
<!--START_ND_TOOLTIPS-->
-<div class=CToolTip id="tt1"><div class=CFile><b>Version: 0.4, Last updated: 7/15/2009</b></div></div><!--END_ND_TOOLTIPS-->
+<div class=CToolTip id="tt1"><div class=CFile><b>Version: 1.0, Last updated: 3/3/2010</b></div></div><!--END_ND_TOOLTIPS-->
</div><!--Index-->
View
@@ -25,11 +25,11 @@
<!--START_ND_TOOLTIPS-->
-<div class=CToolTip id="tt4"><div class=CFile><b>Version: 0.4, Last updated: 7/15/2009</b></div></div><!--END_ND_TOOLTIPS-->
+<div class=CToolTip id="tt4"><div class=CFile><b>Version: 1.0, Last updated: 3/3/2010</b></div></div><!--END_ND_TOOLTIPS-->
<!--START_ND_TOOLTIPS-->
-<div class=CToolTip id="tt5"><div class=CGeneric>Copyright &copy; 2009 &ldquo;Cowboy&rdquo; Ben Alman, Dual licensed under the MIT and GPL licenses. </div></div><!--END_ND_TOOLTIPS-->
+<div class=CToolTip id="tt5"><div class=CGeneric>Copyright &copy; 2010 &ldquo;Cowboy&rdquo; Ben Alman, Dual licensed under the MIT and GPL licenses. </div></div><!--END_ND_TOOLTIPS-->
<!--START_ND_TOOLTIPS-->
Binary file not shown.
View
@@ -1,4 +1,4 @@
1.4
JavaScript
-/srv/projects/jquery-dotimeout/jquery.ba-dotimeout.min.js 1264557446 0 /srv/projects/jquery-dotimeout/jquery.ba-dotimeout.min.js
-/srv/projects/jquery-dotimeout/jquery.ba-dotimeout.js 1264557439 1 jQuery doTimeout: Like setTimeout, but better!
+/srv/projects/jquery-dotimeout/jquery.ba-dotimeout.min.js 1267620842 0 /srv/projects/jquery-dotimeout/jquery.ba-dotimeout.min.js
+/srv/projects/jquery-dotimeout/jquery.ba-dotimeout.js 1267620838 1 jQuery doTimeout: Like setTimeout, but better!
View
Binary file not shown.
View
@@ -12,7 +12,7 @@
$shell['h3'] = ob_get_contents();
ob_end_clean();
-$shell['jquery'] = 'jquery-1.4.1.js';
+$shell['jquery'] = 'jquery-1.4.2.js';
//$shell['jquery'] = 'jquery-1.3.2.js';
$shell['shBrush'] = array( 'JScript' );
@@ -25,15 +25,31 @@
ob_start();
?>
$('#set_timeout2 a.start').click(function(){
- // In one second, do something using `this`!
- $('#set_timeout2 span').doTimeout( 1000, function(){
- this.html( 'done!' );
- });
+ // Execute .css( 'color', 'blue' ), now then in
+ // one second, execute .css( 'color', 'blue' ).
+ $('#set_timeout2 span')
+ .css( 'color', 'red' )
+ .doTimeout( 1000, function(){
+ this.css( 'color', 'blue' )
+ });
});
<?
$shell['script2'] = ob_get_contents();
ob_end_clean();
+ob_start();
+?>
+$('#set_timeout2a a.start').click(function(){
+ // Execute .css( 'color', 'blue' ), now then in
+ // one second, execute .css( 'color', 'blue' ).
+ $('#set_timeout2a span')
+ .css( 'color', 'red' )
+ .doTimeout( 1000, 'css', 'color', 'blue' );
+});
+<?
+$shell['script2a'] = ob_get_contents();
+ob_end_clean();
+
ob_start();
?>
var counter1 = 0;
@@ -129,6 +145,8 @@
<?= $shell['script2']; ?>
+ <?= $shell['script2a']; ?>
+
<?= $shell['script3']; ?>
<?= $shell['script4']; ?>
@@ -216,8 +234,8 @@
<?= htmlspecialchars( $shell['script2'] ); ?>
</pre>
<div id="set_timeout2">
- <p>Set value: <span>???</span>, <span>???</span>, <span>???</span></p>
- <p><a href="#" class="start single">Start</a></p>
+ <p>Set value: <span>Some text</span>, <span>more text</span></p>
+ <p><a href="#" class="start">Start</a></p>
<p>
Much like setTimeout or the example above, except that in the callback, `this`
refers to the jQuery object. Chainable, too!
@@ -226,6 +244,21 @@
<div class="hr"><hr></div>
+<h3>An even easier syntax!</h3>
+<pre class="brush:js">
+<?= htmlspecialchars( $shell['script2a'] ); ?>
+</pre>
+<div id="set_timeout2a">
+ <p>Set value: <span>Some text</span>, <span>more text</span></p>
+ <p><a href="#" class="start">Start</a></p>
+ <p>
+ Much like setTimeout or the example above, except with a much more concise
+ "string method" syntax. Still chainable!
+ </p>
+</div>
+
+<div class="hr"><hr></div>
+
<h3>Basic polling loop (somewhat like setInterval.. but different)</h3>
<pre class="brush:js">
<?= htmlspecialchars( $shell['script3'] ); ?>
@@ -12,17 +12,11 @@
ob_start();
?>
-$('#item-over a')
- .mouseenter(function(){
- $(this).doTimeout( 'hover', 250, function(){
- this.addClass( 'hover' );
- });
- })
- .mouseleave(function(){
- $(this).doTimeout( 'hover', 250, function(){
- this.removeClass( 'hover' );
- });
- });
+$('#item-over a').hover(function(){
+ $(this).doTimeout( 'hover', 250, 'addClass', 'hover' );
+}, function(){
+ $(this).doTimeout( 'hover', 250, 'removeClass', 'hover' );
+});
<?
$shell['script1'] = ob_get_contents();
ob_end_clean();
View
@@ -89,7 +89,7 @@ function draw_shell() {
If console output is mentioned, but your browser has no console, this example is using <a href="http://benalman.com/projects/javascript-debug-console-log/">JavaScript Debug</a>. Click this bookmarklet: <a href="javascript:if(!window.firebug){window.firebug=document.createElement(&quot;script&quot;);firebug.setAttribute(&quot;src&quot;,&quot;http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js&quot;);document.body.appendChild(firebug);(function(){if(window.firebug.version){firebug.init()}else{setTimeout(arguments.callee)}})();void (firebug);if(window.debug&&debug.setCallback){(function(){if(window.firebug&&window.firebug.version){debug.setCallback(function(b){var a=Array.prototype.slice.call(arguments,1);firebug.d.console.cmd[b].apply(window,a)},true)}else{setTimeout(arguments.callee,100)}})()}};">Debug + Firebug Lite</a> to add the Firebug lite console to the current page. Syntax highlighting is handled by <a href="http://alexgorbatchev.com/">SyntaxHighlighter</a>.
</p>
<p>
- All original code is Copyright © 2009 "Cowboy" Ben Alman and dual licensed under the MIT and GPL licenses. View the <a href="http://benalman.com/about/license/">license page</a> for more details.
+ All original code is Copyright &copy; 2010 "Cowboy" Ben Alman and dual licensed under the MIT and GPL licenses. View the <a href="http://benalman.com/about/license/">license page</a> for more details.
</p>
</div>
</div>
View
@@ -1,15 +1,15 @@
/*!
- * jQuery doTimeout: Like setTimeout, but better! - v0.4 - 7/15/2009
+ * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
* http://benalman.com/projects/jquery-dotimeout-plugin/
*
- * Copyright (c) 2009 "Cowboy" Ben Alman
+ * Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Script: jQuery doTimeout: Like setTimeout, but better!
//
-// *Version: 0.4, Last updated: 7/15/2009*
+// *Version: 1.0, Last updated: 3/3/2010*
//
// Project Home - http://benalman.com/projects/jquery-dotimeout-plugin/
// GitHub - http://github.com/cowboy/jquery-dotimeout/
@@ -18,7 +18,7 @@
//
// About: License
//
-// Copyright (c) 2009 "Cowboy" Ben Alman,
+// Copyright (c) 2010 "Cowboy" Ben Alman,
// Dual licensed under the MIT and GPL licenses.
// http://benalman.com/about/license/
//
@@ -37,12 +37,16 @@
// tested with, what browsers it has been tested in, and where the unit tests
// reside (so you can test it yourself).
//
-// jQuery Versions - 1.3.2, 1.4.1
-// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
+// jQuery Versions - 1.3.2, 1.4.2
+// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome 4-5, Opera 9.6-10.1.
// Unit Tests - http://benalman.com/code/projects/jquery-dotimeout/unit/
//
// About: Release History
//
+// 1.0 - (3/3/2010) Callback can now be a string, in which case it will call
+// the appropriate $.method or $.fn.method, depending on where .doTimeout
+// was called. Callback must now return `true` (not just a truthy value)
+// to poll.
// 0.4 - (7/15/2009) Made the "id" argument optional, some other minor tweaks
// 0.3 - (6/25/2009) Initial release
@@ -68,7 +72,8 @@
// when it is executed.
//
// If the callback returns true, the doTimeout loop will execute again, after
- // the delay, creating a polling loop until the callback returns false.
+ // the delay, creating a polling loop until the callback returns a non-true
+ // value.
//
// Note that if an id is not passed as the first argument, this doTimeout will
// NOT be able to be manually canceled or forced. (for debouncing, be sure to
@@ -94,6 +99,9 @@
// delay - (Number) A zero-or-greater delay in milliseconds after which
// callback will be executed.
// callback - (Function) A function to be executed after delay milliseconds.
+ // callback - (String) A jQuery method to be executed after delay
+ // milliseconds. This method will only poll if it explicitly returns
+ // true.
// force_mode - (Boolean) If true, execute that id's doTimeout callback
// immediately and synchronously, continuing any callback return-true
// polling loop. If false, execute the callback immediately and
@@ -124,7 +132,8 @@
// when it is executed.
//
// If the callback returns true, the doTimeout loop will execute again, after
- // the delay, creating a polling loop until the callback returns false.
+ // the delay, creating a polling loop until the callback returns a non-true
+ // value.
//
// Note that if an id is not passed as the first argument, this doTimeout will
// NOT be able to be manually canceled or forced (for debouncing, be sure to
@@ -150,6 +159,10 @@
// delay - (Number) A zero-or-greater delay in milliseconds after which
// callback will be executed.
// callback - (Function) A function to be executed after delay milliseconds.
+ // callback - (String) A jQuery.fn method to be executed after delay
+ // milliseconds. This method will only poll if it explicitly returns
+ // true (most jQuery.fn methods return a jQuery object, and not `true`,
+ // which allows them to be chained and prevents polling).
// force_mode - (Boolean) If true, execute that id's doTimeout callback
// immediately and synchronously, continuing any callback return-true
// polling loop. If false, execute the callback immediately and
@@ -177,6 +190,9 @@
elem,
data = {},
+ // Allows the plugin to call a string callback method.
+ method_base = jquery_data_key ? $.fn : $,
+
// Any additional arguments will be passed to the callback.
args = arguments,
slice_args = 4,
@@ -228,7 +244,14 @@
// A callback (and delay) were specified. Store the callback reference for
// possible later use, and then setTimeout.
data.fn = function( no_polling_loop ) {
- callback.apply( that, aps.call( args, slice_args ) ) && !no_polling_loop
+
+ // If the callback value is a string, it is assumed to be the name of a
+ // method on $ or $.fn depending on where doTimeout was executed.
+ if ( typeof callback === 'string' ) {
+ callback = method_base[ callback ];
+ }
+
+ callback.apply( that, aps.call( args, slice_args ) ) === true && !no_polling_loop
// Since the callback returned true, and we're not specifically
// canceling a polling loop, do it again!

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit c6d9eb9

Please sign in to comment.