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.
 
 
 
 
 
 

162 lines
4.6 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 countdownPrice(el, beginValue, decInSecond) {
var counter = el.find(".counter");
counter.text(beginValue);
if (el.attr("data-tid") ) {
var tid = el.attr("data-tid")
clearInterval(tid);
}
var delay = 1000;
var div = 2.0;
var timerId = setInterval(function(){
var val = parseFloat(counter.text());
var new_val = (val - (decInSecond / div)).toFixed();
counter.text(new_val);
}, delay / div);
el.attr("data-tid", timerId);
}
function countdownPriceStop(el) {
clearInterval(el.attr("data-tid"));
}
function get_price() {
$.ajax({
url: "/dtys/info/",
method: "GET",
success: function(data, textStatus, jqXHR) {
console.log(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();
product.find('.price').text(i.stopped_price);
countdownPriceStop(product);
} else {
product.removeClass("stopped");
product.find(".price").text(i.current_price);
fake_add_to_cart_btn.hide();
add_to_cart_btn.show();
countdownPrice(product, i.current_price, i.price_dec_in_sec);
}
});
}
},
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){
$(".dtys__product").each(function(){
$(this).removeClass("wait");
});
if(me.ETag != x.getResponseHeader('ETag')){
me.params.onModified(data,x,me.lastModified);
me.lastModified = x.getResponseHeader('Last-Modified');
}
me.ETag = x.getResponseHeader('ETag');
},
error: function(jqXHR, textStatus, errorThrown) {
$(".dtys__product").each(function(){
$(this).addClass("wait");
countdownPriceStop($(this));
});
},
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();
this.doUpdate();
};
};
$(function(){
var upd = new Updater();
get_price();
upd.init({
url: "/dtys/updates",
onModified: function(data, x, modified) {
get_price();
}
});
upd.start();
});