Skip to content

Instantly share code, notes, and snippets.

@Sentinel-7
Last active November 19, 2022 12:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Sentinel-7/4b0006c61a9ecf1a555e4734cdd78dca to your computer and use it in GitHub Desktop.
Save Sentinel-7/4b0006c61a9ecf1a555e4734cdd78dca to your computer and use it in GitHub Desktop.
Отложенная загрузка метрики и скриптов
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