Initial commit: существующий сайт + конфигурация разработки
Some checks failed
Deploy to Production / deploy (push) Failing after 4s
Some checks failed
Deploy to Production / deploy (push) Failing after 4s
This commit is contained in:
87
html/js/counter.js
Normal file
87
html/js/counter.js
Normal file
@@ -0,0 +1,87 @@
|
||||
// Простой счетчик посещений
|
||||
class SimpleCounter {
|
||||
constructor() {
|
||||
this.storageKey = 'arseny_site_counter';
|
||||
this.serverFile = '/api/counter.txt'; // Файл для синхронизации
|
||||
this.init();
|
||||
}
|
||||
|
||||
async init() {
|
||||
try {
|
||||
// 1. Получаем локальное значение
|
||||
let localCount = localStorage.getItem(this.storageKey);
|
||||
if (!localCount) {
|
||||
localCount = 1;
|
||||
} else {
|
||||
localCount = parseInt(localCount) + 1;
|
||||
}
|
||||
|
||||
// 2. Сохраняем локально
|
||||
localStorage.setItem(this.storageKey, localCount);
|
||||
|
||||
// 3. Пытаемся синхронизировать с сервером (в фоне, без ожидания)
|
||||
this.syncWithServer(localCount);
|
||||
|
||||
// 4. Показываем на странице
|
||||
this.display(localCount);
|
||||
|
||||
} catch (error) {
|
||||
console.log('Счетчик работает в локальном режиме');
|
||||
this.displayFallback();
|
||||
}
|
||||
}
|
||||
|
||||
async syncWithServer(count) {
|
||||
try {
|
||||
// Отправляем данные на сервер
|
||||
await fetch(this.serverFile, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ count: count })
|
||||
});
|
||||
} catch (error) {
|
||||
// Игнорируем ошибки - работает локальная версия
|
||||
}
|
||||
}
|
||||
|
||||
display(count) {
|
||||
// Ищем все элементы для отображения счетчика
|
||||
const elements = document.querySelectorAll('[data-counter]');
|
||||
|
||||
elements.forEach(element => {
|
||||
const format = element.dataset.format || 'simple';
|
||||
|
||||
switch(format) {
|
||||
case 'simple':
|
||||
element.textContent = `Посетителей: ${this.formatNumber(count)}`;
|
||||
break;
|
||||
case 'number':
|
||||
element.textContent = this.formatNumber(count);
|
||||
break;
|
||||
case 'message':
|
||||
element.textContent = `Вы посетитель №${this.formatNumber(count)}`;
|
||||
break;
|
||||
}
|
||||
|
||||
element.classList.add('counter-loaded');
|
||||
});
|
||||
}
|
||||
|
||||
displayFallback() {
|
||||
const elements = document.querySelectorAll('[data-counter]');
|
||||
elements.forEach(element => {
|
||||
element.textContent = 'Счетчик загружается...';
|
||||
});
|
||||
}
|
||||
|
||||
formatNumber(num) {
|
||||
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
// Автоматический запуск при загрузке страницы
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
new SimpleCounter();
|
||||
});
|
||||
Reference in New Issue
Block a user