Commit 209a6866 authored by Vadim's avatar Vadim

Version 3.6.4

Оптимизирована подгрузка новых счетчиков
parent 6c8bfeb2
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Plugin Name: Bg Az-Counter Plugin Name: Bg Az-Counter
Plugin URI: https://bogaiskov.ru Plugin URI: https://bogaiskov.ru
Description: Подсчет количества посещений страниц на базе stat.azbyka.ru Description: Подсчет количества посещений страниц на базе stat.azbyka.ru
Version: 2.6.3 Version: 2.6.4
Author: VBog Author: VBog
Author URI: https://bogaiskov.ru Author URI: https://bogaiskov.ru
License: GPL2 License: GPL2
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
if ( !defined('ABSPATH') ) { if ( !defined('ABSPATH') ) {
die( 'Sorry, you are not allowed to access this page directly.' ); die( 'Sorry, you are not allowed to access this page directly.' );
} }
define('BG_COUNTER_VERSION', '2.6.3'); define('BG_COUNTER_VERSION', '2.6.4');
define('BG_COUNTER_LOG', dirname(__FILE__ ).'/bg_counter.log'); define('BG_COUNTER_LOG', dirname(__FILE__ ).'/bg_counter.log');
define('BG_COUNTER_STAT_COUNTERS','https://stat.azbyka.ru/counters'); define('BG_COUNTER_STAT_COUNTERS','https://stat.azbyka.ru/counters');
......
...@@ -13,7 +13,11 @@ jQuery( document ).ready(function() { ...@@ -13,7 +13,11 @@ jQuery( document ).ready(function() {
var socket = new ReconnectingWebSocket(request, null, { timeoutInterval: 10000 }); var socket = new ReconnectingWebSocket(request, null, { timeoutInterval: 10000 });
socket.onopen = function() { socket.onopen = function() {
if (bg_counter.debug) console.log(" Соединение установлено: "+request); if (bg_counter.debug) console.log(" Соединение установлено: "+request);
GetAllCounters(); // Обновляет счетчики каждые 3 сек, если добавлены элементы.
let timerAllCountersId = setTimeout(function tickAllCounters() {
GetAllCounters();
timerAllCountersId = setTimeout(tickAllCounters, bg_counter.updatetime?bg_counter.updatetime:3000);
}, bg_counter.updatetime?bg_counter.updatetime:3000);
}; };
// Обработка ошибок // Обработка ошибок
if (bg_counter.debug) { if (bg_counter.debug) {
...@@ -29,12 +33,17 @@ jQuery( document ).ready(function() { ...@@ -29,12 +33,17 @@ jQuery( document ).ready(function() {
console.log('Код: ' + event.code + ' причина: ' + event.reason); console.log('Код: ' + event.code + ' причина: ' + event.reason);
}; };
} }
} else GetAllCounters(); } else {
// Обновляет счетчики каждые 3 сек, если добавлены элементы.
// Обновляет счетчики после прокрутки страницы, если добавлены элементы. let timerAllCountersId = setTimeout(function tickAllCounters() {
jQuery(window).on('scroll', function() { GetAllCounters();
GetAllCounters(); timerAllCountersId = setTimeout(tickAllCounters, bg_counter.updatetime?bg_counter.updatetime:3000);
}); }, bg_counter.updatetime?bg_counter.updatetime:3000);
}
// Обновляет счетчики после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// GetAllCounters();
// });
/********************************************************************************* /*********************************************************************************
Просомтр счетчиков читающих в реальном времени. Просомтр счетчиков читающих в реальном времени.
...@@ -194,57 +203,64 @@ GET /counters/project/test/author/1/book/3 ...@@ -194,57 +203,64 @@ GET /counters/project/test/author/1/book/3
function GetAllCounters() { function GetAllCounters() {
var elem = jQuery('span.bg-az-counter'); var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) { if( typeof elem == 'undefined' ) {
return; return;
} }
var elem_num = 0;
if (elem.length > bg_counter_elements) { if (elem.length > bg_counter_elements) {
bg_counter_elements = elem.length; elem.each (function () {
jQuery('span.bg-az-counter').each (function () { if (elem_num >= bg_counter_elements) {
var el = jQuery(this); var el = jQuery(this);
// bg_counter_elements = el.length; var type = el.attr('data-type');
var type = el.attr('data-type'); var id = el.attr('data-ID');
var id = el.attr('data-ID'); var project = el.attr('data-project');
var project = el.attr('data-project'); if (project == "") request = bg_counter.counterurl;
if (project == "") request = bg_counter.counterurl; else {
else { if (project) project = '/project/'+project;
if (project) project = '/project/'+project; else project = bg_counter.project;
else project = bg_counter.project;
if (!type || !id) var request = bg_counter.counterurl+project;
if (!type || !id) var request = bg_counter.counterurl+project; else var request = bg_counter.counterurl+project+"/"+type+"/"+id;
else var request = bg_counter.counterurl+project+"/"+type+"/"+id;
}
} var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest(); xhr.open("GET", request, true);
xhr.open("GET", request, true); xhr.onreadystatechange = function() {
xhr.onreadystatechange = function() { if (xhr.readyState != 4) return;
if (xhr.readyState == 4 && xhr.status == 200) { if (xhr.status == 200) {
if (xhr.responseText) { if (xhr.responseText) {
var response = JSON.parse(xhr.responseText); var response = JSON.parse(xhr.responseText);
if (response.success) { if (response.success) {
if (bg_counter.debug) console.log('GET REQUEST: '+request); if (bg_counter.debug) console.log('GET REQUEST: '+request);
if (bg_counter.debug) console.log(JSON.stringify(response.data)); if (bg_counter.debug) console.log(JSON.stringify(response.data));
el.find('span.bg-az-counter-views').text(bg_counter_number_format(response.data.total)); el.find('span.bg-az-counter-views').text(bg_counter_number_format(response.data.total));
el.find('span.bg-az-counter-now').text(addDelimiter(response.data.now)); el.find('span.bg-az-counter-now').text(addDelimiter(response.data.now));
} else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR '+xhr.status+': '+response.error);
el.find('span.bg-az-counter-views').text('0');
el.find('span.bg-az-counter-now').text('0');
}
} else { } else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR '+xhr.status+': '+response.error); if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-views').text('0'); el.find('span.bg-az-counter-views').text(' - ');
el.find('span.bg-az-counter-now').text('0'); el.find('span.bg-az-counter-now').text(' - ');
} }
} else { } else {
if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!'); if (bg_counter.debug) console.warn('ERROR '+xhr.status+': '+xhr.statusText);
el.find('span.bg-az-counter-views').text(' - '); el.find('span.bg-az-counter-views').text('-');
el.find('span.bg-az-counter-now').text(' - '); el.find('span.bg-az-counter-now').text('-');
} }
} }
} xhr.onerror = function(err) {
xhr.onerror = function(err) { console.warn(err.type +" "+ err.target.status + ". Check if the server is running!");
console.warn(err.type +" "+ err.target.status + ". Check if the server is running!"); el.find('span.bg-az-counter-views').text(' - ');
el.find('span.bg-az-counter-views').text(' - '); el.find('span.bg-az-counter-now').text(' - ');
el.find('span.bg-az-counter-now').text(' - '); }
}
xhr.send(); xhr.send();
}
elem_num++;
}); });
bg_counter_elements = elem.length;
} }
} }
/********************************************************************************* /*********************************************************************************
......
...@@ -6,11 +6,16 @@ var bg_counter_ratings = 0; ...@@ -6,11 +6,16 @@ var bg_counter_ratings = 0;
jQuery( document ).ready(function() { jQuery( document ).ready(function() {
getAllRates(); // Обновлять рейтинги каждые 3 сек, если добавлены элементы.
// Обновлять рейтинги после прокрутки страницы, если добавлены элементы. let timerAllRatesId = setTimeout(function tickAllRates() {
jQuery(window).on('scroll', function() {
getAllRates(); getAllRates();
}); timerAllRatesId = setTimeout(tickAllRates, bg_counter.updatetime?bg_counter.updatetime:3000);
}, bg_counter.updatetime?bg_counter.updatetime:3000);
// Обновлять рейтинги после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// getAllRates();
// });
if (!bg_counter.ID) return; // У объекта нет ID if (!bg_counter.ID) return; // У объекта нет ID
if (jQuery("div").is(".bg_counter_rating") == false) return; // На странице нет счетчика if (jQuery("div").is(".bg_counter_rating") == false) return; // На странице нет счетчика
...@@ -142,7 +147,6 @@ function getAllRates() { ...@@ -142,7 +147,6 @@ function getAllRates() {
bg_counter_ratings = elem.length; bg_counter_ratings = elem.length;
jQuery('span.bg-az-counter').each (function () { jQuery('span.bg-az-counter').each (function () {
var el = jQuery(this); var el = jQuery(this);
// bg_counter_ratings = el.length;
var type = el.attr('data-type'); var type = el.attr('data-type');
var id = el.attr('data-ID'); var id = el.attr('data-ID');
var project = el.attr('data-project'); var project = el.attr('data-project');
...@@ -155,7 +159,8 @@ function getAllRates() { ...@@ -155,7 +159,8 @@ function getAllRates() {
xhr.open("GET", request, true); xhr.open("GET", request, true);
if (bg_counter.debug) console.log('GET REQUEST: '+request); if (bg_counter.debug) console.log('GET REQUEST: '+request);
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) { if (xhr.readyState != 4) return;
if (xhr.status == 200) {
if (xhr.responseText) { if (xhr.responseText) {
var response = JSON.parse(xhr.responseText); var response = JSON.parse(xhr.responseText);
if (response.success) { if (response.success) {
...@@ -170,6 +175,9 @@ function getAllRates() { ...@@ -170,6 +175,9 @@ function getAllRates() {
if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!'); if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-score').text(' - '); el.find('span.bg-az-counter-score').text(' - ');
} }
} else {
if (bg_counter.debug) console.warn('ERROR '+xhr.status+': '+xhr.statusText);
el.find('span.bg-az-counter-score').text('-');
} }
} }
xhr.send(); xhr.send();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment