jQuery | finish() with Example

The finish() is an inbuilt method in jQuery which is used to stop the animations running at the present time.
Syntax:

$(selector).finish();

Parameter: It does not accept any parameter.
Return Value: It returns the selected element with its final values.

jQuery code to show the working of finish() method:



filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   
<head>
    <script 
    </script>
    <script>
        <!-- jQuery code to show the working of this method -->
        $(document).ready(function() {
            $("#b1").click(function() {
                $("div").animate({
                    height: 200
                }, 4000);
                $("div").animate({
                    width: 200
                }, 4000);
            });
            $("#b2").click(function() {
                $("div").finish();
            });
        });
    </script>
    <style>
        div {
            background: green;
            height: 100px;
            width: 100px;
            padding: 30px;
        }
    </style>
</head>
   
<body>
   
    <div></div>
   
    <p>
        <!-- this button will start the animation -->
        <button id="b1">Start </button>
        <!-- this button will finish the animation -->
        <button id="b2">Stop</button>
    </p>
   
</body>
   
</html>

chevron_right


Output:
Before clicking on the “Start” button-

After clicking on the start button animation will start with its specified speed and when stop button is clicked then it immediately finish the animation and return the element to its final height and width value.



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.