forum

Home / DeveloperSection / Forums / How to jQuery - stop/ start a function?

How to jQuery - stop/ start a function?

Anonymous User 1798 28-Jan-2015

When you hover over the ".homepage-modules" container is it possible to stop the "next" function so it stops cycling the active state and when you hover off the container the "next" function starts firing again? I'm a bit stumped at the moment and not sure of a way to do this

$('.homepage-modules div:first').addClass('active');
 
$.fn.cycle = function (timeout, cls) {
    var l = this.length,
        current = 0,
        prev = 0,
        elements = this;
 
    if (this.filter('.active').length> 0) {
        current = this.index(this.filter('.active')[0])+ 1;
        prev = current- 1;
    }
 
    function next() {
        elements.eq(prev).removeClass('active');
        elements.eq(current).addClass('active');
        prev = current;
        current = (current+ 1) % l;
        setTimeout(next,timeout);
    }
    setTimeout(next,timeout);
    return this;
};
 
$('.homepage-modules div').cycle(2000, 'active');


Updated on 28-Jan-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By