Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance fix #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,62 @@ <h1 style="font-size: 14px; font-weight: normal; margin: 0; padding: 0;">Где
});
</script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var seo = document.querySelector('.index-seo').innerText;
var items = seo.split(' ');

(function flashWord() {
var i = 0;
function run(item) {
var t = setTimeout(function () {
clearTimeout(t);
document.querySelector('.index-seo').innerHTML = seo.replace(new RegExp(' ' + item + ' ', 'g'), ' <span style="color: #fff">' + item + '</span> ');
if (items.length) {
i = getRandom(0, items.length - 1);
run(items[i].replace(/[^A-Za-zА-Яа-яёЁ0-9]/gim, ''));
}
}, 100);
var UPDATE_TIME = 100;

function getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function run(prevNodes, map) {
var t = setTimeout(function() {
clearTimeout(t);
var nodes = map[getRandom(0, Math.floor(map.length / 2))]
prevNodes.forEach(node => {
node.style.color = 'inherit';
});

nodes.forEach(node => {
node.style.color = '#fff';
});

run(nodes, map);
}, UPDATE_TIME);
}

function flashWord() {
var seoElement = document.querySelector('.index-seo');
var seoText = seoElement.innerText;
var items = seoText.split(' ');

var uniqItems = items.reduce((uniq, item) => {
if (!uniq[item]) {
uniq[item] = `_${Math.random().toString().slice(2, 10)}`
}
function getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
return uniq;
}, {});

seoElement.innerHTML = Object.entries(uniqItems).reduce((total, [word, className]) => {
var cleanWord = word.replace(/[^A-Za-zА-Яа-яёЁ0-9]/gim);
if (word !== cleanWord) {
return total;
}
run(items[i]);
})();

return total.replace(new RegExp(` ${cleanWord} `, 'g'),
` <span class="${className}">${cleanWord}</span> `);
}, seoText);

var nodesMap = Object.values(uniqItems)
.map(className => Array.from(document.querySelectorAll(`.${className}`)), {})
.filter(nodes => nodes.length)
.sort((a, b) => b.length - a.length);

run([], nodesMap);
}

document.addEventListener('DOMContentLoaded', function () {
flashWord();

(function countMovies() {
var date = new Date();
Expand Down