Commit 37badd20 authored by Vadim's avatar Vadim

Version 2.6.4

Оптимизирована подгрузка новых счетчиков
parent 0e8c96ab
...@@ -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.2 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.2'); 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);
// Обновляет счетчики каждые 3 сек, если добавлены элементы.
let timerAllCountersId = setTimeout(function tickAllCounters() {
GetAllCounters(); 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,9 +33,17 @@ jQuery( document ).ready(function() { ...@@ -29,9 +33,17 @@ jQuery( document ).ready(function() {
console.log('Код: ' + event.code + ' причина: ' + event.reason); console.log('Код: ' + event.code + ' причина: ' + event.reason);
}; };
} }
} else GetAllCounters(); } else {
// Обновляет счетчики каждые 3 сек, если добавлены элементы.
bg_counter_elements_reloaded_on_scroll(); let timerAllCountersId = setTimeout(function tickAllCounters() {
GetAllCounters();
timerAllCountersId = setTimeout(tickAllCounters, bg_counter.updatetime?bg_counter.updatetime:3000);
}, bg_counter.updatetime?bg_counter.updatetime:3000);
}
// Обновляет счетчики после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// GetAllCounters();
// });
/********************************************************************************* /*********************************************************************************
Просомтр счетчиков читающих в реальном времени. Просомтр счетчиков читающих в реальном времени.
...@@ -190,9 +202,15 @@ GET /counters/project/test/author/1/book/3 ...@@ -190,9 +202,15 @@ GET /counters/project/test/author/1/book/3
**********************************************************************************/ **********************************************************************************/
function GetAllCounters() { function GetAllCounters() {
jQuery('span.bg-az-counter').each (function () { var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
var elem_num = 0;
if (elem.length > bg_counter_elements) {
elem.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');
...@@ -208,7 +226,8 @@ function GetAllCounters() { ...@@ -208,7 +226,8 @@ function GetAllCounters() {
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 && 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) {
...@@ -226,6 +245,10 @@ function GetAllCounters() { ...@@ -226,6 +245,10 @@ function GetAllCounters() {
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(' - ');
} }
} else {
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-now').text('-');
} }
} }
xhr.onerror = function(err) { xhr.onerror = function(err) {
...@@ -234,7 +257,11 @@ function GetAllCounters() { ...@@ -234,7 +257,11 @@ function GetAllCounters() {
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;
}
} }
/********************************************************************************* /*********************************************************************************
Отображает значения счетчика на странице Отображает значения счетчика на странице
...@@ -280,22 +307,3 @@ function bg_counter_number_format (num) { ...@@ -280,22 +307,3 @@ function bg_counter_number_format (num) {
return num; return num;
} }
/*********************************************************************************
Обновляет счетчики после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function bg_counter_elements_reloaded_on_scroll() {
jQuery(window).on('scroll', function() {
var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
if (elem.length > bg_counter_elements) {
bg_counter_elements = elem.length;
GetAllCounters();
// getAllRates();
}
});
}
...@@ -6,9 +6,17 @@ var bg_counter_ratings = 0; ...@@ -6,9 +6,17 @@ var bg_counter_ratings = 0;
jQuery( document ).ready(function() { jQuery( document ).ready(function() {
// Обновлять рейтинги каждые 3 сек, если добавлены элементы.
let timerAllRatesId = setTimeout(function tickAllRates() {
getAllRates();
timerAllRatesId = setTimeout(tickAllRates, bg_counter.updatetime?bg_counter.updatetime:3000);
}, bg_counter.updatetime?bg_counter.updatetime:3000);
// Обновлять рейтинги после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// getAllRates(); // getAllRates();
bg_counter_ratings_reloaded_on_scroll(); // });
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; // На странице нет счетчика
...@@ -130,9 +138,15 @@ function getRate(type, id) { ...@@ -130,9 +138,15 @@ function getRate(type, id) {
function getAllRates() { function getAllRates() {
var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
if (elem.length > bg_counter_ratings) {
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');
...@@ -145,7 +159,8 @@ function getAllRates() { ...@@ -145,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) {
...@@ -160,11 +175,14 @@ function getAllRates() { ...@@ -160,11 +175,14 @@ 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();
}); });
}
} }
...@@ -226,21 +244,3 @@ function sendRate(type, id, number) { ...@@ -226,21 +244,3 @@ function sendRate(type, id, number) {
xhr.send('{"rating": '+number+'}'); xhr.send('{"rating": '+number+'}');
} }
/*********************************************************************************
Обновляет рейтинги после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function bg_counter_ratings_reloaded_on_scroll() {
jQuery(window).on('scroll', function() {
var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
if (elem.length > bg_counter_ratings) {
bg_counter_ratings = elem.length;
getAllRates();
}
});
}
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