Good evening, there is a js code that, when you hover over an item in the list, shows a submenu. But if you quickly remove and remove the mouse from the limits of the block, you get an accordion effect, the menu performs as many full code cycles as I did. How to disable the execution of the code before the last time is over?
function dropDownMenu() { $(this).children('img').toggleClass('chevron-rotate'); $(this).children('.dropdown-ul').slideToggle(250); } $('.dropdown-li').on({ mouseenter: dropDownMenu, mouseleave: dropDownMenu }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="menu"> <li><a href="index.html">Главная</a></li> <li class="dropdown-li"> <a href="#">О нас</a> <img src="img/chevron-white.png" alt=""> <ul class="dropdown-ul"> <li><a href="#">О компании</a></li> <li><a href="#">Мероприятия</a></li> <li><a href="#">Сертификаты</a></li> <li><a href="#">Награды</a></li> <li><a href="#">Вопрос-ответ</a></li> </ul> </li> <li class="dropdown-li"> <a href="#">Программы</a> <img src="img/chevron-white.png" alt=""> <ul class="dropdown-ul"> <li><a href="#">Для похудения</a></li> <li><a href="#">Для здоровья</a></li> </ul> </li> <ul> I tried to write a condition using flags, but did not help. I work with js quite recently, I can not understand what does not work
function dropDownMenu() { var flag = true; if (flag){ $(this).children('img').addClass('chevron-rotate'); $(this).children('.dropdown-ul').slideToggle(250); var flag = false; } else{ $(this).children('img').removeClass('chevron-rotate'); $(this).children('.dropdown-ul').slideToggle(250); var flag = true; } }