Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a function from running?

Tags:

jquery

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.

like image 842
Ryan Avatar asked Jan 31 '11 12:01

Ryan


People also ask

How do you stop a function from running Python?

One of the most appropriate functions that we can use to exit from a Python program is the sys. exit() function. This function is available in the sys module and when called it raises the SystemException in Python that then triggers the interpreter to stop further execution of the current python script.

How do you disable a function in JavaScript?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .


4 Answers

.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.

like image 196
mekwall Avatar answered Sep 25 '22 15:09

mekwall


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.

like image 21
amurra Avatar answered Sep 23 '22 15:09

amurra


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!

like image 20
bysanchy Avatar answered Sep 24 '22 15:09

bysanchy


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);

like image 22
Robert Koritnik Avatar answered Sep 22 '22 15:09

Robert Koritnik