Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cancel/stop jquery fadeOut after begin

I've got a very simple page that shows a status update when a user clicks on specific entries on the page.

This is all working fine. The first click updates the id='sts' with the correct output, after 6 seconds this fades away.

However whilst it's fading if the user clicks another link the DIV is updated with the new text, but it continues to fade away based on the original fadeout time out.

Anyway to have the DIV updates start the fade counter again ?

This is what I'm currently using to do the div update.

$('.first').click(function () {
    $("#sts").html('first update 1').show().fadeOut(6000);
});

$('.next').click(function () {
    $("#sts").html('second update 2').show().fadeOut(6000);
});

$('.last').click(function () {
    $("#sts").html('dinal update 3').show().fadeOut(6000);
});

Thanks

like image 950
MacMan Avatar asked Dec 15 '14 14:12

MacMan


People also ask

How do you fadeOut in jQuery?

The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector).fadeOut(speed,callback); The optional speed parameter specifies the duration of the effect.

How do you stop a function call in jQuery?

jQuery stop() Method The stop() method stops the currently running animation for the selected elements.

How do you fade in Javascript?

jQuery Effect fadeOut() Method The fadeOut() method gradually changes the opacity, for selected elements, from visible to hidden (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeIn() method.


2 Answers

You need to use .stop( [clearQueue ] [, jumpToEnd ]).

Stop the currently-running animation on the matched elements.

$("#sts").stop(true,true).html('first update 1').show().fadeOut(6000);

Working Demo using stop

As per A.Wolff suggestion:

You can use .finish() as an alternative to .stop(true,true)

Finish():

Stop the currently-running animation, remove all queued animations, and complete all animations for the matched elements.

$("#sts").finish().html('first update 1').show().fadeOut(6000);

Working Demo using finish

like image 134
Milind Anantwar Avatar answered Sep 16 '22 14:09

Milind Anantwar


You could stop the current fadeOut and after that start it again.

For stopping the current fadeOut you could do something like this:

$("#sts").stop().animate({opacity:'100'}).html('first update 1').fadeOut(6000);
like image 26
twain Avatar answered Sep 19 '22 14:09

twain