JQuery onclick div hide/show/reset for the next time clicked - javascript

Simple question from a newbie:
How do I reset/restart a function after completing the time of (500
ms) in order to repeat the function each time I click on a specific
button?
FIRST PART OF THE CODE WORKS
my div is disappearing after 500ms fading out
$('.animate-this').click(function () {
$('p').show().delay(500).fadeOut();
});
SECOND PART IS MISSING
I would need to reset the fuction in order to repeated immediately...
I guess there is a very simple solution for that..

fadeOut accepts a callback function. You can use that
function animate() {
$('p').show().delay(500).fadeOut('slow', function() {
animate()
});
}

Related

Add a delay to jQuery fadeIn function

I am trying to add a delay to my fadeIn function in jQuery. The purpose of the code is that when 'topmods' or 'dailyskins' button is pressed it will hide/show the other parent div.
Currently when i press the div 'topmods' is does hide the div 'dailyskins' though the content of 'topmods' goes below 'dailyskins' for a split second until 'dailyskins' has finished fading out.
I think this would be solved by adding a delay to both fadeIn and Out, though i don't know how to add this,
Please could you add a delay of 200ms to each of the fadeIn segments.
jQuery(document).ready(function(){
$("#topmods").hide();
jQuery('#dropdailyskin').live('click', function(event) {
jQuery('#dailyskins').fadeIn('show');
});
jQuery('#dropdailyskin').live('click', function(event) {
jQuery('#topmods').fadeOut('show');
});
jQuery('#dropdownmods').live('click', function(event) {
jQuery('#dailyskins').fadeOut('show');
});
jQuery('#dropdownmods').live('click', function(event) {
jQuery('#topmods').fadeIn('show');
});
Thanks
Just add the .delay to your code like this
$("idhere").delay(1000).fadeIn(500);
its in milliseconds.
You can add a timeout before executing your function using this:
setTimeout(function(){jQuery('#dailyskins').fadeIn('show')}, 200);
or you can use.
$("idhere").delay(1000).show();

why setInterval() cycle goes faster every time?

I'm building a custom slider on Javascript , and I want that every time the user clicks on a div of the slider, the slider should stop for X seconds.
My code is:
$(document).ready(function () {
var ciclo;
var index_slide = 1;
function startSlidercicle() {
ciclo = setInterval( function() {
// Slider code goes here
}, 3000);
}
//Here I start the slider animation
startSlidercicle();
//When the user clicks on a div called 'slide', stop the cycle and start again the animation cycle
$('.slide').on('click', function() {
clearInterval(ciclo);
setTimeout(startSlidercicle(), 3000);
});
});
But the problem is that everytime I click and stop the slider, the cycle starts faster and faster. How can I fix it?
Instead of:
clearInterval(ciclo);
setTimeout(startSlidercicle(), 3000);
or:
clearInterval(ciclo);
setTimeout(startSlidercicle, 3000);
I changed the code to be:
clearInterval(ciclo);
startSlidercicle();
And now the slider just works fine. I think that, in the first two proposals, every time I click on the div, a new function is created, "overlapping" over the existing cycle and, thus, it looks like the slider speeds up, but its just one cycle starting over another.
You need to change this:
clearInterval(ciclo);
setTimeout(startSlidercicle(), 3000);
to this:
clearInterval(ciclo);
setTimeout(startSlidercicle, 3000);
In your existing code, you are calling startSlidercirle immediately and it is not waiting until the setTimeout() fires because you have the () after the function name. That means to execute it immediately and pass the result of executing that to setTimeout(). You want to just pass the function reference to setTimeout() which is done by just having the name of the function with no () after it. This is a common mistake.

Jquery stop function no works fine

I create this function for show div with function call , but i nedd execute this function and stop in one second after show :
<script>
function repit_clouds(request)
{
if (request=='no')
{
stop();
}
else
{
if (request=='ok')
{
var interval = setInterval(function() {
$("#header_sun").fadeIn(4000);
$("#header_sun").fadeOut(4000);
},10000);
}
}
}
$(document).ready(function()
{
$("#header_sun").fadeIn(4000).delay(4000).fadeOut(4000);
repit_clouds('ok');
$("#header_background_clouds").fadeOut(2000).css("display","#none");
$("#header_background_night").fadeIn(2000).css("background","#000").show(4000).fadeIn(2500);
repit_clouds('no');
});
</script>
The problem it´s with repit_clouds function , when i send the value no the function must stop , but no works fine and continue as if the request==ok , i don´t know why no stop
umm, what exactly do you think stop() does? Did you perhaps mean:
$('#header_sun').stop()
which would pause any existing animations on that element?
Or did you perhaps also want to stop the interval timer:
clearInterval(interval);
which of course also requires that interval be declared outside of the function so that its value isn't lost each time you call the function.
I note also that your .css() functions won't be queued by the .fadeOut() or .show() calls - .css() isn't an animation function so those changes will always happen immediately.
Likewise the two calls to repit_clouds() won't be queued either - if you expect those to be done after the previous animations then you need to investigate the jQuery .queue() function, or trigger them during "animation complete callbacks".

jQuery Isotope queue shuffle/randomize animation

I would like to set the jQuery Isotope shuffle method to to perform an animated shuffle of the loaded DOM elements every 30 seconds that someone is on the page with the plugin loaded.
I have had success tying the animation to a .hover() event, but I cannot seem to get it to fire when I use setInterval() or .queue(). I want the animation to fire regardless of user interaction/input.
var iso_shuffle = function() {
$('#isotope').isotope('shuffle');
}
setInterval(iso_shuffle(), 2500);
Why does the previous code not trigger the randomization, yet this does:
$('#isotope').hover(function() {
iso_shuffle()
});
Cheers
iso_shuffle() calls the function immediately. The function returns nothing. So your setInterval is actually doing the equivalent of:
setInterval(undefined, 2500);
You want to use the function name as the callback for setInterval:
setInterval(iso_shuffle, 2500);
You need to pass the function itself, not its return value:
setInterval(iso_shuffle, 2500);

fadeout function along with slidetoggle function

When I submit my form, the last function I have is:
$("#message").show().delay(5000).fadeOut();
This will show my thank you message for 5 seconds, and then fade the "message" div out.
I then tried adding this function below it:
$("#slide_panel").slideToggle("slow");
because I want the form (which is inside the #slide_panel div) to close / slide up AFTER the 5 second delay.... but when I add this function, its almost like the 5 second delay doesn't exist and the success message shows for about half a second and then the whole contact form dissapears as its supposed to.
What is wrong with my code?
$("#message").show().delay(5000).fadeOut();
$("#slide_panel").slideToggle("slow");
delay function applies only to animation queue. you can use the following code by passing a call back function to fadeOut()
$("#message").show().delay(5000).fadeOut('fast', function(){
$("#slide_panel").slideToggle("slow");
});
Now the slideToggle will run once the animation is completed.
You can update the code to the following ...
$("#message").show().delay(5000).fadeOut(function(){
$("#slide_panel").slideToggle("slow");
});
That is, add the slideToggle of SlidePanel in the message callback. For more ideas, check http://jsfiddle.net/sf2Nr/1/
This has to do with the asynchronous behavior of the animation functions in jQuery.
In order to activate the slideToggle after the fadeOut delay, you must call it from the fadeOut's callback function, i.e.:
$("#message").show().delay(5000).fadeOut(0, function() {
$("#slide_panel").slideToggle("slow");
});

Categories

Resources