Skip to content

Instantly share code, notes, and snippets.

@Sentinel-7
Last active November 19, 2022 12:51
Embed
What would you like to do?
Отложенная загрузка метрики и скриптов
var loadedMetrica = false;
window.addEventListener('scroll', loadMetrica);
window.addEventListener('click', loadMetrica);
window.addEventListener('mousemove', loadMetrica);
function loadMetrica() {
if (!loadedMetrica) {
setTimeout(function() {
/* ТУТ ВАШ КОД И СКРИПТЫ, метрика например*/
},500);
}
loadedMetrica = true;
window.removeEventListener('scroll', loadMetrica);
window.removeEventListener('click', loadMetrica);
window.removeEventListener('mousemove', loadMetrica);
};
// Реагирует на события скролла, свайпа, движения мышкой и затем срабатывает.
var fired = false;
window.addEventListener('scroll', () => {
if (fired === false) {
fired = true;
setTimeout(() => {
// Сюда вставляете метрики без тегов <script>
}, 1000)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment