Skip to content

Instantly share code, notes, and snippets.

@leylaKapi
Created October 2, 2017 14:06
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 leylaKapi/306cd6f173acd72cbe3b3649d025d9e3 to your computer and use it in GitHub Desktop.
Save leylaKapi/306cd6f173acd72cbe3b3649d025d9e3 to your computer and use it in GitHub Desktop.
Spining loader ajax
function spining_calculator(target_div) {
if (target_div == ''){
var indicator = '<table id="indicator" style="width:100%;height:100%;" class="ajax_loading indicator_small"><tr><th style="text-align:center"><img src="/images/indicator-small.gif" /></th></tr></table>';
$('body').prepend(indicator);
} else {
var width = $(target_div).width();
var height = $(target_div).height();
var indicator = '<table id="indicator" style="width:'+width+'px; height:'+height+'px;" class="ajax_loading indicator_small"><tr><th style="text-align:center"><img src="/images/indicator-small.gif" /></th></tr></table>';
$(target_div).prepend(indicator);
}
}
$('#product_list .caption .added_to_cart').click(function() {
spining_calculator($('#product_list'));
var price_id, product_id, quantity;
quantity = $(this).prev().val();
product_id = $(this).attr('data-product-id');
price_id = $(this).attr('data-price-id');
$.ajax({
type: 'POST',
url: '/order_items',
data: {
quantity: quantity,
product_id: product_id,
price_id: price_id
},
success: function() {
alert('Add to cart');
},
error: function() {
alert('Try again');
},
complete: function() {
$('#indicator').remove();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment