corenav
corenav copied to clipboard
How to wait until coreNavigation function is available?
Hi, I have timing issue. How to use a check to wait until the coreNavigation function in coreNavigation.js is available?
I tried using this code below, but is not working. (using timeout does work but is not reliable approach)
Maybe in below code the issue is defining the function (typeof coreNavigation === 'function')
What is best way to do this, or using JQuery?
$nav = jQuery.noConflict();
$nav(document).ready(function($) {
// On doc ready for modern browsers
document.addEventListener('DOMContentLoaded', (e) => {
// Scope all logic related to what you want to achieve by using a function
const waitForMyFunction = () => {
// Use a timeout id to identify your process and purge it when it's no longer needed
let timeoutID;
// Check if your function is defined, in this case by checking its type
if (typeof coreNavigation === 'function') {
// We no longer need to wait, purge the timeout id
window.clearTimeout(timeoutID);
console.log("found coreNavigation");
// 'coreNav' function is defined, invoke the function
coreNavigationInit();
} else {
// console.log("checking");
// 'coreNavigation' is undefined, try again in 0.25 secs
timeoutID = window.setTimeout(waitForMyFunction, 250);
}
};
// Initialize
waitForMyFunction();
});
function coreNavigationInit() {
$('nav').coreNavigation({
menuPosition: "left",
container: true,
animated: false,
responsideSlide: false,
dropdownEvent: 'click',
onInit: function(){
$('ul.tabs > li > a').click(function(e){
e.preventDefault();
var tab_id = $(this).closest('li.tab-link').attr('data-tab');
$('ul.tabs li').removeClass('active');
$('.tab-content').removeClass('active');
$(this).closest('li.tab-link').addClass('active');
$("#"+tab_id).addClass('active');
})
}
});
};
});`
This is more of a feature request/requesting advice than an issue