Ryan
Ryan

Reputation: 1791

How to stop a function from running?

I have a function but it keeps on running even if I call a stop(). The function name is function slide() { //codes here }

slide().stop();

How to stop it from running?
And how to run it again after stopping it?
So I will be having a full control on it.

Upvotes: 7

Views: 95869

Answers (4)

bysanchy
bysanchy

Reputation: 105

Had the same problem and after reading the last answer by amurra this is what I did...

Declared a global variable by attaching it to the 'window' object. This will act as a flag and decide whether to keep calling the 'slide_it()' function or to stop it (return)

window.slide_it = true;

$(element).on({

    mouseenter:
        function() {
            window.slide_it = true;
            slide_it();
        }

    mouseleave:
        function() {
            window.slide_it = false;
        }

});

function slide_it () {

    if( !window.slide_it )
        return false;

    $(element).stop().animate( 
        {
            'left':'-=5px'
        },
        100,
        function() { slide_it() }
    );

}

Once you enter the mouse over the $(element) the slide_it() function will iterate itself. And it will stop only if window.slide_it is set to false. So you just set window.slide_it to false when "mouseleave"s

Hope it helps someone else having the issue!

Upvotes: 2

mekwall
mekwall

Reputation: 28974

.stop is a method for stopping animations that is running asynchronously (in the background) on the current element. It doesn't interrupt/stop any other code that isn't using the effects library.

Instead you should take a look at .queue which lets you setup custom queues that you can clear or dequeue.

There already exist very good answers on how to use .queue so I'll point you to that question instead: Can somebody explain jQuery queue to me? - Look at the answer by gnarf.

Upvotes: 12

Robert Koritnik
Robert Koritnik

Reputation: 105029

If your function is a long running javascript process you should put some kind of outside hooks in it to stop it's execution process and continue when desired. If you have a while loop in it you should be checking against an outside condition to stop its execution. If you don't have a loop in the regular sense of it you will have to find some other way of checking function's running state conditioning.

It's generally not possible to interrupt a running function in Javascript. Not without any additional code that is.

Long running functions will be interrupted by browsers by asking the user whether they want the script to continue execution or rather stop it (browser assumes execution has hanged as in an infinite loop).

If you do have a long running function in Javascript you should split fnctionality into several parts and use javascript threading technique that delays execution of short functions one after another by means of window.setTimeout(function, delay);

Upvotes: 1

amurra
amurra

Reputation: 15401

If it hits a certain condition in your slide function just use a return; statement to exit out of the function. Then to start it again just recall the function slide(). You can't use code from outside of the function to make it exit, it has to be within the function.

Upvotes: 8

Related Questions