const download = (fileUrl, filename) => {
fetch(fileUrl)
.then(response => response.blob())
.then(data => {
const tempUrl = URL.createObjectURL(data);
const link = document.createElement('a');
link.href = tempUrl;
link.download = filename;
link.addEventListener('click', () => {
setTimeout(() => {
URL.revokeObjectURL(tempUrl);
});
});
link.click();
});
};
window.addEventListener('DOMContentLoaded', () => {
download('MY_URL', 'MY_FILENAME.txt');
});
const combine = ({
entries = [],
createItem = entry => entry,
getParent = entry => entry,
getChild = entry => entry,
getKey = entry => entry.id
}) => {
const cache = new Map();
return entries.reduce((accumulator, entry) => {
const parent = getParent(entry);
const child = getChild(entry);
const key = getKey(parent);
if (!cache.has(key)) {
const branch = {
...createItem(parent),
children: []
};
cache.set(key, branch);
accumulator.push(branch);
}
cache.get(key).children.push(createItem(child));
return accumulator;
}, []);
};
const result = combine({
entries: columnList,
createItem: entry => ({
key: `${entry.name}-${entry.id}`,
title: entry.name
}),
getParent: entry => entry.table
});
const numbers = Array.from({ length: 1e2 + 1 }, (_, index) => index);
const log = (value, base) => Math.log(value) / Math.log(base);
const numbersStartsWith = (numbers, starts) => numbers.filter(number => {
const divider = Math.pow(10, Math.floor(log(Math.abs(number), 10)));
const digit = Math.floor(number / divider);
return starts.includes(digit);
});
const filtered = numbersStartsWith(numbers, [1, 2, 5]);
console.log(filtered);
// [1, 2, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 100]
-let randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
-button1.style.backgroundColor = randomColor;
-button2.style.backgroundColor = randomColor;
-button3.style.backgroundColor = randomColor;
-button4.style.backgroundColor = randomColor;
+let randomColor = () => '#' + Math.floor(Math.random()*16777215).toString(16);
+button1.style.backgroundColor = randomColor();
+button2.style.backgroundColor = randomColor();
+button3.style.backgroundColor = randomColor();
+button4.style.backgroundColor = randomColor();
const buttons = document.querySelectorAll('[id^="color"]');
const randomNumber = (max) => Math.round(Math.random() * max);
const generateColor = () => `hsl(${randomNumber(360)}, ${randomNumber(100)}%, ${randomNumber(100)}%)`;
for (const button of buttons) {
button.style.setProperty('background-color', generateColor());
}
useEffect
;deps
передаете пустой массив;const PageSize = () => {
const [pageSize, setPageSize] = useState([document.documentElement.scrollWidth, document.documentElement.scrollHeight]);
const updatePageSize = () => setPageSize([document.documentElement.scrollWidth, document.documentElement.scrollHeight]);
useEffect(() => {
window.addEventListener('resize', updatePageSize);
return () => {
window.removeEventListener('resize', updatePageSize);
};
}, []);
return (
<div>
<p>page width is {pageSize[0]}</p>
<p>page height is {pageSize[1]}</p>
</div>
);
};
mousemove
. Приложение работает, всё хорошо, но потом, по некоторой причине, происходит размонтирование и через некоторое время снова монтирование. В таком случае, если не удалить прослушку, будет уже 2 события, что и является утечкой памяти.<script src="" type="module"></script>
. Если среда исполнения node, то в package.json
"type": "module"
.