You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
3.7 KiB

function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function get_price() {
$.ajax({
url: "/dtys/info/",
method: "GET",
// beforeSend: function(xhr) {
// // var csrftoken = getCookie("csrftoken");
// // if (csrftoken) {
// // xhr.setRequestHeader("X-CSRFToken", csrftoken);
// // }
// },
success: function(data, textStatus, jqXHR) {
// console.log("DTYS data", data);
if (data.dtys_list.length > 0) {
var active = data.dtys_list;
active.forEach(function(i, idx, list) {
var product = $("#dtys_product_" + i.id);
var add_to_cart_btn = product.find(".add-to-cart");
var fake_add_to_cart_btn = product.find(".fake-add-to-cart");
if (i.stopped) {
product.addClass("stopped");
fake_add_to_cart_btn.show();
add_to_cart_btn.hide();
} else {
product.removeClass("stopped");
product.find(".price").text(i.current_price);
fake_add_to_cart_btn.hide();
add_to_cart_btn.show();
}
});
}
},
error: function(jqXHR, textStatus, errorThrow) {
console.log("что-то пошло не так, сервер не отвечает");
}
});
};
var Updater = function(){
this.params = {
period: 3000,
url: '',
onModified: function(data,x,modified){},
bgPause: false
};
this.interval = null;
this.ETag = '';
this.lastModified = '';
this.init = function(params){
var me = this;
this.params = $.extend(this.params, params);
if(this.params.bgPause){
$(window).blur(function(){ me.pause() });
$(window).focus(function(){ me.resume() });
}
};
this.start = function(){
var me = this;
this.interval = setInterval(function(){ me.doUpdate() }, this.params.period);
};
this.doUpdate = function(){
var me = this;
$.ajax(this.params.url, {
success: function(data,status,x){
if(me.ETag != x.getResponseHeader('ETag')){
me.params.onModified(data,x,me.lastModified);
me.lastModified = x.getResponseHeader('Last-Modified');
}
me.ETag = x.getResponseHeader('ETag');
},
beforeSend: function(x){
if(me.ETag != '') { x.setRequestHeader('If-None-Match', me.ETag); }
},
cache: false
});
};
this.pause = function(){
clearInterval(this.interval);
this.interval = null;
};
this.resume = function(){
if(this.interval != null) return;
this.start();
};
};
$(function(){
var upd = new Updater();
upd.init({
url: "/dtys/etg/",
onModified: function(data, x, modified) {
get_price();
console.log(data, x, modified);
}
});
upd.start();
});