Create a $.wait(delay, function(){...}) that returns a promise.
While jQuery has jQuery.delay() that is used in UI and queues, this is very different in that any Function can be delayed!
Promises then allow .done() and .pipe() features to be used.
<body>
<!--
SyntaxHighlighter code prettify
-->
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js"></script>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/current/styles/shCoreDefault.css"/>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeRDark.css" />
<script type="text/javascript">SyntaxHighlighter.all();</script>
<div id="body">
<div id="header"></div>
<div id="contents">
<div id="intro">
<p>
Create a <span class='jquery'>$.wait()</span> function that uses <strong><a href="http://api.jquery.com/delay/" target="_blank">jQuery Deferred/Promise</a></strong> features.
</p>
</div>
<h2 id="getting-started">
<span class='code'>$.wait</span>( delay, <function>, ... ) : Promise
</h2><br>
<div class="contents">
<div class="bullet">
<div class="description">
jQuery has a function <span class='jquery'>jQuery.delay()</span> that is used to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. In contrast, <code><span class='code'>$.wait()</span></code> is very different. Any function call can be delayed, even if it is not in a jQuery queue!<br>
<br>
Best of all... since the <code><span class='code'>$.wait()</span></code> function returns a Promise, developers can easily use the <span class='jquery'>Promise::pipe()</span> and <span class='jquery'>Promise::done()</span> features to chain actions and easily defer custom processing to a future...
( function($) {
/**
* Similar to the jQuery.delay() function but this one supports any arbitrary delay
* and allows optional, additional parameters to passed [later] to the
* resolved handler [assigned via .done() or .then()].
*
* A special configuration allows the 1st optional parameter to be a function
* reference so the wait(delay,function(){...}) syntax can be easily used.
*
* Here are the possible call options:
*
* wait( delay )
* wait( delay, ...params )
* wait( delay, func2Call )
* wait( delay, func2Call, ...func2Params )
*
* @param delay Number of milliseconds to wait before resolving/projecting the promise;
* Default value === 30 msecs
*
* @param args Optional listing of parameters
*/
$.wait = function() {
var args = [].slice.call( arguments, 1 ),
delay = arguments[0];
/**
* If the first variable param is a Function reference, then auto-call
* that function with/without any subsequent optional params
*/
var doInlineCallback = function () {
var func = args.length ? args[0] : null,
result = null;
if ( $.isFunction(func) ) {
// 1) Remove function element,
// 2) Call function, save response, and
// 3) Clear arguments
args.shift();
result = func.apply(null, args);
args = [ ];
}
return result;
};
return new $.Deferred( function(dfd) {
var timer = setTimeout( function(){
clearInterval(timer);
// Call the specified function (if any)
var response = doInlineCallback();
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.