---
title: "How to jQuery - stop/ start a function?"  
description: "How to jQuery - stop/ start a function?"  
author: "Anonymous User"  
published: 2015-01-28  
updated: 2015-01-28  
canonical: https://www.mindstick.com/forum/12910/how-to-jquery-stop-start-a-function  
category: "asp.net"  
tags: ["jquery", "javascript"]  
reading_time: 2 minutes  

---

# How to jQuery - stop/ start a function?

When you hover over the ".homepage-[modules](https://www.mindstick.com/interview/266/what-is-the-use-of-http-modules)" [container](https://www.mindstick.com/forum/159610/how-to-connect-a-windows-container-to-a-service-running-on-localhost) is it possible to stop the "next" [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) so it stops [cycling](https://www.mindstick.com/blog/300095/reasons-why-cycling-is-good-for-you) the [active state](https://www.mindstick.com/interview/2327/when-an-app-is-said-to-be-in-active-state) and when you hover off the container the "next" function starts firing again? I'm a [bit](https://www.mindstick.com/forum/33411/how-we-tell-programmatically-if-i-m-running-in-64-bit-jvm-or-32-bit-jvm) 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');
```

## Replies

### Reply by Anonymous User

You should store the result of your setTimeout calls in a variable within your plugin. When the mouse is over your element you can use clearTimeout to stop the timer running, and when the mouse moves out you can start the timer again.

```
$.fn.cycle = function (timeout, cls) {    var l = this.length,        current = 0,        prev = 0,        elements = this,        timerId;     if (this.filter('.active').length > 0) {        current = this.index(this.filter('.active')[0]) + 1;        prev = current - 1;    }     this.parent().hover(function(){        clearTimeout(timerId);        },    function(){        timerId = setTimeout(next, timeout);              })     function next() {        elements.eq(prev).removeClass('active');        elements.eq(current).addClass('active');        prev = current;        current = (current + 1) % l;        timerId = setTimeout(next, timeout);    }    timerId = setTimeout(next, timeout);    return this;}; 
```


---

Original Source: https://www.mindstick.com/forum/12910/how-to-jquery-stop-start-a-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
