jQuery | stop() with Examples

The stop() method is an inbuilt method in jQuery which is used to stop the currently running animations for the selected element.

Syntax:

$(selector).stop(stopAll, goToEnd);

Parameters: This method accepts two parameters as mentioned above and described below:

  • stopAll: It is optional parameter and the value of this parameter is boolean. This parameter is used to specify whether or not to stop the queued animations as well. The default value of this parameter is false.
  • goToEnd: It is optional parameter and the value of this parameter is boolean. This parameter is used to specify whether or not to complete all animations immediately. The default value of this parameter is false.

Return Value: This method returns the selected element with stop method applied.

Below examples illustrate the stop() method in jQuery:

Example 1: This example does not contains any parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The stop Method</title>
        <script src=
        </script>
          
        <!-- jQurey code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("#gfg_start").click(function() {
                    $("div").animate({
                        height: 300
                    }, 1000);
                    $("div").animate({
                        width: 300
                    }, 1000);
                });
                $("#gfg_stop").click(function() {
                    $("div").stop();
                });
            });
        </script>
        <style>
            div {
                background: green;
                height: 60px;
                width: 60px;
            }
            button {
                margin-bottom:30px;
            }
        </style>
    </head>
    <body>
        <!-- click on this button and animation will start -->
        <button id="gfg_start">Start</button>
        <!-- click on this button and animation will stop -->
        <button id="gfg_stop">Stop</button>
        <div></div>
    </body>
</html>

chevron_right


Output:

Example 2: This example contains parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title> The stop Method</title>
        <script src=
        </script>
          
        <script>
            $(document).ready(function() {
                var div = $("div");
                $("#start").click(function() {
                    div.animate({
                        height: 280
                    }, "slow");
                    div.animate({
                        width: 280
                    }, "slow");
                    div.animate({
                        height: 120
                    }, "slow");
                    div.animate({
                        width: 120
                    }, "slow");
                });
                $("#stop").click(function() {
                    div.stop(true, true);
                });
            });
        </script>
        <style>
            div {
                background: green;
                height: 100px;
                width: 100px;
            }
            button {
                margin-bottom:30px;
            }
        </style>
    </head>
    <body>
        <!-- click on this button and animation will start -->
        <button id="start">Start </button>
        <!-- click on this button and animation will stop -->
        <button id="stop">Stop </button>
        <div></div>
    </body>
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.