Stopping and resetting slider set interval

javascript jquery setinterval

106 просмотра

1 ответ

I've searched around different posts but still can't understand what to do entirely.

Here is my setInterval code for a slider. As it's a slider the interval begins on page load. Once I mouseleave the interval speed is faster, which means I have two going on at the same time I'm assuming, however I'm not sure how to smoothly stop it at mouseenter and get it going again at the same pace on mouseleave.

var interval = setInterval(slideInterval, 4500);

$('.main-container').on('mouseenter', function(){
    clearInterval(interval);

});
, $('.main-container').on('mouseleave', function() {
    setInterval(slideInterval, 4500);
});
Автор: Meds86 Источник Размещён: 08.11.2019 11:29

Ответы (1)


0 плюса

its because when you leave the element you creating a new interval without a var assigment for clearing it next.

and its will produce more and more intervals on same DOM Element. heres the fixed code:

var A = document,
    B = window,
    a = 0,
    b = function(c){return A.getElementsByTagName(c)[0]},
    d = setInterval(function(){b('div').innerHTML = ++a;}, 1000);
b('div').onmouseover = function(){clearInterval(d);}
b('div').onmouseleave = function (){d = setInterval(function(){
  b('div').innerHTML = ++a;
}, 1000);}
div{width:500px;height:150px;border:1px solid black;}
<div></div>

i did it compcted in javascript without jquery

Автор: Baruch B. Размещён: 20.08.2016 03:36
Вопросы из категории :
32x32