Difference between calling stop(true,true) and finish method ?
Using jQuery, various effects or animations can be performed on DOM elements. These can be simple standard effects provided by the built-in methods of the jQuery library or some custom effects created using the jQuery animate() method. The animate method allows changing the CSS properties of the target element(s) and can be chained multiple times to perform more than one animation.
The state of these animations can be controlled using functions like stop and finish. Both these methods are similar in operation as they are used to stop the animations applied on a particular element, but the finish method has a significant difference as compared to the stop(true, true) method.
stop(true, true): If multiple animations are applied to an element, they are placed in the effects queue for that element. On calling the stop(true, true) method the currently running animation is stopped immediately after jumping to its end state i.e. the CSS properties used in the current animation are set to their target values and the effects queue of that element is cleared, thus removing any further animations applied on the element.
- The optional clearQueue parameter (boolean) is set to true and is used to clear the effects queue of the element
- The optional jumpToEnd parameter (boolean) is set to true that stops the current animation and jumps to its end state
finish(): Similar to the stop(true, true) method, the finish() method also stops the currently running animation after jumping to its end value and clears the effects queue of the matched element(s). However, it differs in the way that it causes all the queued animations to jump to their end values as well. This means that the CSS properties of all the queued animations are set to their target values that would have been applied if the animations were allowed to finish naturally.
Note: If the optional queue parameter (string) is available, then only the animations represented by that string will be stopped.
Let’s understand the difference better through the following example.
Example: In this example, the start, stop(true, true), and finish buttons are created to depict the usage of the stop(true, true) and the finish method on the animated text.
Explanation: At first, the start button is clicked to depict the complete animation applied to the text. After that, on starting the animation again, the stop(true, true) button is clicked to stop the currently running animation (animation moving the text to the right) and immediately moves the text to the right of the box i.e. jumps to its end state and clears all the queued animations. Further, after restarting the animation, as soon as the finish button is clicked, the current animation is stopped, the effects queue is cleared and all the queued animations are completed immediately, thus moving the text to its final position (at the top right corner).
Please Login to comment...