Currently I have something setup where a user clicks on an image and based off that image, a div will fade in / fade out.
If the user goes crazy and clicks on a bunch of the images at the same time, multiple divs load rather than just the last requested one.
I've tried to illustrate my problem here. http://jsfiddle.net/BBgsf/
Clicking on any of those images will load the corresponding div with the number. But if you click on different images before the animation is completed, it loads the other divs as well.
jQuery
$(".flow-chart img").click(function () {
var div_class = $(this).data("class");
$(".hide_show:visible").fadeOut('slow', function() {
$("."+div_class).fadeIn("slow");
});
});
HTML
<div class="1 hide_show">1</div>
<div class="2 hide_show" style="display: none">2</div>
<div class="3 hide_show" style="display: none">3</div>
How can I prevent the multiple divs from loading rather than just one at a time?
Learning jQueryready(function(){ $("*"). dblclick(function(e){ e. preventDefault(); }); }); Above jQuery code will disable double click event for all the controls on the webpage.
To prevent multiple button clicks in React: Set an onClick prop on the button, passing it a function.
on() method is the preferred method for attaching event handlers to a document. To answer your question about multiple submits, another new addition in JQuery 1.7 is the . one() handler which, attaches an event handler to an object but only allows it to be fired once. This will allow you to prevent multiple submits.
jQuery Effect show() Method The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).
Though you already got an answer.. You can just check to see if there are any elements that are animating using the :animated selector.. and if there are return false
$(".flow-chart img").click(function() {
if ($(".hide_show").filter(':animated').length > 0) {
return false;
}
var div_class = $(this).data("class");
$(".hide_show:visible").fadeOut('slow', function() {
$("." + div_class).fadeIn("slow");
});
});
http://jsfiddle.net/FaKBs/
One way to do this is to keep track of if an animation is currently active. Here is a simple way to do this: http://jsfiddle.net/QCWgR/
var active = false;
$(".flow-chart img").click(function () {
if (active) {
return;
}
active = true;
var div_class = $(this).data("class");
$(".hide_show:visible").fadeOut('slow', function() {
// note the callback that sets active to false at end of animation
$("."+div_class).fadeIn("slow", function() { active = false; });
});
});
With this approach, the first click has to complete the animation cycle before the next one will happen.
To keep active out of global namespace you can run the whole block inside an anonymous closure like so:
http://jsfiddle.net/QCWgR/2/
(function () {
var active = false;
$(".flow-chart img").click(function () {
if (active) {
return;
}
active = true;
var div_class = $(this).data("class");
$(".hide_show:visible").fadeOut('slow', function() {
$("."+div_class).fadeIn("slow", function() { active = false; });
});
});
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With