Is there a way to turn off the function while using another?

I have two functions. The first is responsible for the fact that when you scroll down at the navigation, the active buttons change. The second is responsible for the quick navigation transition.

Question: Is it possible to “turn off” using the second one when using the second one?

thank

  • In such a formulation, no, JS is executed in one stream (true for browsers). - user207618

2 answers 2

In such a formulation, no, JS is executed in one stream (true for browsers).
But you can do with the flag:

 let flag = true, log = null; function firstFn(){ // Если флаг равен false, происходит выход if(!flag) return; log.innerHTML = `Первая функция работает: ${Math.random()}!`; } function secondFn(){ // При старте, блокируем флаг flag = false; new Promise((a, d) => { log.innerHTML = `Пока первая функция не работает, проверь.`; setTimeout(a, 3000); }).then(() => { // Как только какой-то долгий процесс закончился, разблокируем флаг flag = true; log.innerHTML = `Первая функция вновь в строю, проверь.`; }); } document.addEventListener('DOMContentLoaded', e => { document.querySelector('#f').addEventListener('click', firstFn); document.querySelector('#s').addEventListener('click', secondFn); log = document.querySelector('#log'); }); 
 <input type='button' id='f' value='Выполни меня полностью!' /> | <input type='button' id='s' value='Я долгая функция, пока я работаю, первая не будет работать!' /><br /> <span id='log'></span> 

  • here, rather than a flag, but a promise, without it the flag is useless - Grundy
  • @Grundy, in this example - yes, but this is an artificial delay, the vehicle may have another (something about the scroll - until the animation ends, the flag blocks the execution). - user207618

Javascript is single-threaded, that is, only one function can be executed at a time.

  • But I have two then performed - Nikita Shchypylov
  • One - look at the scrollTop () and change the class in accordance with the distance, and the other on click - Nikita Shchypylov
  • @ Nikita Schipilov, in turn - yes. As soon as the process enters the inactivity phase, only then the planned operations are performed, not earlier. - user207618
  • @Nikita Schipilov, add an infinite loop to any of your functions and you will see that while it is executed, nothing else will be executed - Grundy