On my site I have an addEventListener that executes the following code. The variables/constants are defined like this: const header_search_form = document.querySelector('.header-search-form');.
What I need is that this part of the code is executed with a delay of 2 seconds: document.getElementById("header-search-field-input").focus();. The other code should be executed normally, with the if condition also being respected. I already tried some things, but that didn't work.
header_search_button.addEventListener('click', onClickOpensearch);
function onClickOpensearch(){
if(header_search_container.classList.contains('open')){
header_parent.classList.remove('open');
header_search_background.classList.remove('open');
document.getElementById("header-search-field-input").blur();
document.getElementById("header-search-field-input").value = "";
} else {
header_parent.classList.add('open');
header_search_background.classList.add('open');
document.getElementById("header-search-field-input").focus();
}
}
It would be great if someone could show me how to change the code!