Skip to content

Instantly share code, notes, and snippets.

@n0nick
Created September 5, 2011 09:12
Show Gist options
  • Save n0nick/1194515 to your computer and use it in GitHub Desktop.
Save n0nick/1194515 to your computer and use it in GitHub Desktop.
function ajax(url, settings) {
var promise;
// actual call
promise = $.ajax(url, settings).promise();
// delay magic
promise = $.when(promise, $('<div/>').appendTo('body').fadeOut(3000)).pipe(function(value){return value[0];});
// here you can set generic handlers for success, fail and always
// e.g.
// promise.always(function(){
// console.log('AJAX call completed.');
// });
// see http://api.jquery.com/category/deferred-object/ for more info.
return promise;
}
// example AJAX call
ajax('/example/', { foo: 3, bar: 14 })
.then(function(o){
console.log(o.result+ '!');
})
.fail(function(){
console.log(':(');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment