I want to implement the interval in which the number increases when the button is clicked.
There are multiple buttons and one element with increasing number.
The problem is, if I click another button during an interval, I can't stop the interval that was already in progress.
Please help me.
HTML
<ul>
<li><a href="javascript:;" class="btn" data-length="3">1</a></li>
<li><a href="javascript:;" class="btn" data-length="8">2</a></li>
<li><a href="javascript:;" class="btn" data-length="5">3</a></li>
</ul>
<div class="counter">0</div>
JS
const interval = (e) => {
setTimeout(() => {
$('.counter').html(e)
}, 500 * e);
}
$('.btn').on('click', function(){
var $this = $(this),
length = $this.data('length');
for (let i = 1; i <= length; i++) {
(function(e){
interval(e)
})(i)
}
})