let index = -1;
setInterval(el => {
const show = !el.classList.contains('_active');
index = (index + show) % infoModal.length;
el.innerText = infoModal[index].title;
el.classList.toggle('_active', show);
}, 1000, document.querySelector('.modal__title'));
Если, к примеру, нужно скрывать блок каждый раз через5 сек, а показывать блок через рандомный промежуток времени?
(function updateText(el, i) {
const show = el.classList.toggle('_active');
i = (i + show) % infoModal.length;
el.textContent = infoModal[i].title;
setTimeout(updateText, 1000 + !show * Math.random() * 3000, el, i);
})(document.querySelector('.modal__title'), -1);
const entries = Object.entries(obj); for (const entry of entries) { if (!isObject(entry)) { result = Object.assign(result, Object.fromEntries(entry)); } else return cloneDeep(entry);
const a = {};
const b = {};
a.b = b;
b.a = a;
cloneDeep(a); // бесконечный цикл
let cities = [];
function myGame() {
const input = document.getElementById('input_city');
const output = document.getElementById('output_city');
//Если массив пустой добавим 1е слово без всяких проверок.
if (!cities.length) {
console.log('Верно идем дальше');
cities.push(input.value);
input.value = ''
console.log(cities)
return
}
const lastCity = cities[cities.length - 1];
const lastLetter = lastCity[lastCity.length - 1];
const firstLetter = input.value.substr(0, 1); // первая буква слова
if (lastLetter.toLowerCase() === firstLetter.toLowerCase()) {
console.log('Верно идем дальше');
cities.push(input.value);
input.value = ''
} else {
console.log('Херня, давай сначала.')
}
console.log(cities);
}
const arr = []; // создали массив
arr.push = function() { // перезаписываем конкретно его push
Array.prototype.push.apply(this, arguments); // вызвали оригинальный push со всеми аргументами
// тут делаем еще что-то
};
function replaceText(node, replacer) {
if (node.nodeType === Node.ELEMENT_NODE) {
node.childNodes.forEach(n => replaceText(n, replacer));
} else if (node.nodeType === Node.TEXT_NODE) {
node.textContent = replacer(node.textContent);
}
}
function replaceText(node, replacer) {
const iter = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
for (let n = null; n = iter.nextNode();) {
n.nodeValue = replacer(n.nodeValue);
}
}
replaceText(document.body, str => str.replace(/\d/g, 'hello, world!!'));
const arrs = [ arr1, arr2 ];
.const result = arrs.reduce((acc, n) => (
n.forEach((m, i) => Object.assign(acc[i] ??= {}, m)),
acc
), []);
function* zip(data, defaultValue = null) {
const iterators = Array.from(data, n => n[Symbol.iterator]());
for (let doneAll = false; doneAll = !doneAll;) {
const values = [];
for (const n of iterators) {
const { value, done } = n.next();
values.push(done ? defaultValue : value);
doneAll &&= done;
}
if (!doneAll) {
yield values;
}
}
}
const result = Array.from(zip(arrs), n => Object.assign({}, ...n));
// main.js
export default {
title: "sun",
subtitle: "earth",
r: "mars"
}
<h1></h1>
<h2></h2>
<p></p>
//index.js
import data from './main.js';
// Пишем функцию, чтобы не менять каждый элемент вручную
const changeHtmlText = (querySelector, data) => {
const $element = document.querySelector(querySelector);
$element.textContent = data
}
changeHtmlText("h1", data.title);
changeHtmlText("h2", data.subtitle);
changeHtmlText("p", data.r);