Skip to content

Instantly share code, notes, and snippets.

@fgbreel
Created March 18, 2016 17:50
Show Gist options
  • Save fgbreel/956d9041dba25afd295c to your computer and use it in GitHub Desktop.
Save fgbreel/956d9041dba25afd295c to your computer and use it in GitHub Desktop.
JS ajax background loader
var post = {
init: function() { $('.post').on('click', this.loadPost); },
updatePost: function(result, event) {
var $post = $(event.currentTarget);
$post.fadeIn({
duration: 400,
start: function(){
$post.find('.post-title').text(result.title);
$post.find('.post-content').text(result.content);
$post.find('.post-category').text(result.category);
$post.find('.post-meta').text();
}
});
},
loadPost: function(event) {
$.ajax('recent', {
dataType: 'json',
timeout: 4000,
success: function(result) { post.updatePost(result, event) },
error: function(request, errorType, errorMessage) {
alert('Error: ' + errorType + ' with message: ' + errorMessage);
},
beforeSend: function(){
$(event.currentTarget).fadeOut();
},
});
}
}
$(document).ready(function() {
post.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment