Skip to content

Instantly share code, notes, and snippets.

@rvagg
Forked from ded/ender.delay.js
Created November 19, 2011 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvagg/1378659 to your computer and use it in GitHub Desktop.
Save rvagg/1378659 to your computer and use it in GitHub Desktop.
ender deferred
!function ($) {
function deferred(exec) {
var mock = {}
, chain = []
, key
, $el = $(this)
, push = function (key) {
mock[key] = function () {
chain.push({ key: key, arguments: arguments })
return mock
}
}
for (key in $el) {
!isFinite(key) && push(key)
}
exec(function () {
for (var i = 0; i < chain.length; i++) {
$el = $el[chain[i].key].apply($el, chain[i].arguments)
}
})
return mock
}
function fade(fadeFn) {
return function(n, cb) {
var self = this
return deferred.call(this, function(fn) {
fadeFn.call(self, n, function() {
cb && cb()
fn()
})
})
}
}
$.ender({
delay: function(n) {
return deferred.call(this, function(fn) {
setTimeout(fn, n)
})
}
, fadeIn: fade($.fn.fadeIn)
, fadeOut: fade($.fn.fadeOut)
}, true)
}(ender);
$('#el').fadeOut().fadeIn().delay(500).css({width: '100px'})
@rvagg
Copy link
Author

rvagg commented Nov 19, 2011

@ded I know you didn't ask for this and I'm butting in but it's too interesting to let go. The concept could be made generic so you can trigger the deferred calls by other means. This example breaks the existing Morpheus fadeIn() and fadeOut() API because you get back the mock element instead of a stoppable; but you get the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment