Я пытаюсь создать функцию прокрутки страниц на своем веб-сайте с помощью jQuery. Все работает, за исключением того, что я хочу, чтобы он принимал первое событие прокрутки и блокировал любые другие события прокрутки, пока анимация не завершится (700 мс), прежде чем принять новое событие прокрутки.
Я попытался установить тайм-аут, но это приостановило процесс, а затем продолжило после тайм-аута, а не блокировало любой ввод из события прокрутки.
<section style = "max-height: 101vh; min-height: 100vh; height:100%; overflow: hidden">
<div id = "container" data-scroll = "0" style = "height: 100vh; overflow-y:hidden;">
<div class = "scrollbit" style = "transition: all; transition-duration: 700ms;">
<div data-id = "0" class = "active" style = "height: 100vh; background: red; transition-duration: 700ms;"></div>
<div data-id = "1" style = "height: 100vh; background: green; transition-duration: 700ms;"></div>
<div data-id = "2" style = "height: 100vh; background: yellow; transition-duration: 700ms;"></div>
<div data-id = "3" style = "height: 100vh; background: blue; transition-duration: 700ms;"></div>
</div>
</div>
</section>
<script src = "https://code.jquery.com/jquery-3.6.0.min.js"
integrity = "sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4 = " crossorigin = "anonymous">
</script>
<script>
function doScroll(direction) {
var active = $('.active').attr('data-id')
active = Number(active)
if (event.deltaY == -100) {
if (active != 0) {
active = Number(active) - Number(1);
$('.scrollbit').animate({ textIndent: 0 }, {
step: function (now, fx) {
$(this).css('transform', 'translateY(-' + (Number(active) * 100) + 'vh')
}, duration: 'slow',
}, 'linear');
$('.active').removeClass('active')
$('[data-id = "' + active + '"]').addClass('active')
}
}
if (event.deltaY == 100) {
var elem = '[data-id = "' + (Number(active) + 1) + '"]';
if ($(elem).length != 0) {
active = Number(active) + Number(1);
$('.scrollbit').animate({ textIndent: 0 }, {
step: function (now, fx) {
$(this).css('transform', 'translateY(-' + (Number(active) * 100) + 'vh')
}, duration: 'slow',
}, 'linear');
$('.active').removeClass('active')
var target = '[data-id = "' + active + '"]';
$('[data-id = "' + active + '"]').addClass('active')
}
}
}
window.addEventListener("wheel", event => doScroll(event));
</script>
<style>
body {
margin: 0;
}
</style>



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете использовать переменную boolean, чтобы заблокировать прокрутку, пока функция animate не станет complete.
<section style = "max-height: 101vh; min-height: 100vh; height:100%; overflow: hidden">
<div id = "container" data-scroll = "0" style = "height: 100vh; overflow-y:hidden;">
<div class = "scrollbit" style = "transition: all; transition-duration: 700ms;">
<div data-id = "0" class = "active" style = "height: 100vh; background: red; transition-duration: 700ms;"></div>
<div data-id = "1" style = "height: 100vh; background: green; transition-duration: 700ms;"></div>
<div data-id = "2" style = "height: 100vh; background: yellow; transition-duration: 700ms;"></div>
<div data-id = "3" style = "height: 100vh; background: blue; transition-duration: 700ms;"></div>
</div>
</div>
</section>
<script src = "https://code.jquery.com/jquery-3.6.0.min.js"
integrity = "sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4 = " crossorigin = "anonymous">
</script>
<script>
let stopScroll = false;
function doScroll(direction) {
if (stopScroll) return;
var active = $('.active').attr('data-id')
active = Number(active)
if (event.deltaY == -100) {
if (active != 0) {
stopScroll = true;
active = Number(active) - Number(1);
$('.scrollbit').animate({ textIndent: 0 }, {
step: function (now, fx) {
$(this).css('transform', 'translateY(-' + (Number(active) * 100) + 'vh')
},
complete: function() {
stopScroll = false;
},
duration: 'slow',
}, 'linear');
$('.active').removeClass('active')
$('[data-id = "' + active + '"]').addClass('active')
}
}
if (event.deltaY == 100) {
var elem = '[data-id = "' + (Number(active) + 1) + '"]';
if ($(elem).length != 0) {
stopScroll = true;
active = Number(active) + Number(1);
$('.scrollbit').animate({ textIndent: 0 }, {
step: function (now, fx) {
$(this).css('transform', 'translateY(-' + (Number(active) * 100) + 'vh')
},
complete: function() {
stopScroll = false;
},
duration: 'slow',
}, 'linear');
$('.active').removeClass('active')
var target = '[data-id = "' + active + '"]';
$('[data-id = "' + active + '"]').addClass('active')
}
}
}
window.addEventListener("wheel", event => doScroll(event));
</script>
<style>
body {
margin: 0;
}
</style>