Created
March 3, 2021 11:57
-
-
Save kernusr/0a6af19635972363cff44e4843c9935d to your computer and use it in GitHub Desktop.
Ожидание загрузки счетчика Яндекс.Метрики
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Ожидание загрузки счетчика Яндекс.Метрики | |
* @param {?(number|string)} ymCounterNum - номер счетчика, если известен | |
* @param {function} callback - получает аргументами объект и номер счетчика | |
* @param {number} interval - интервал проверки готовности счетчика | |
*/ | |
function waitForYm(ymCounterNum, callback, interval) { | |
if (!callback) return; | |
if (!ymCounterNum) { | |
let metrikaObj = (window.Ya && (window.Ya.Metrika || window.Ya.Metrika2)) || null; | |
ymCounterNum = (metrikaObj && metrikaObj.counters && (metrikaObj.counters() || [0])[0].id) || 0; | |
} | |
let ymCounterObj = window['yaCounter' + ymCounterNum] || null; | |
if (ymCounterObj) return (callback(ymCounterObj, ymCounterNum), undefined); | |
setTimeout(function () { waitForYm(ymCounterNum, callback, interval); }, interval || 250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment