Codeworks client log in
latest tweet
Check out our awesome new canvas that arrived today, we'll be making a few more of them to put round the office! http://t.co/oZlesYKD
Have a rummage around our archive = there's some real hidden treasures in there!

news, announcements & our blog...

Preventing multiple clicks in jquery posted by Adam B, 21.09.2011

Here's another top tip from the Codeworks camp. Often Jquery is used to animate a particular element on a page after the user has clicked on something. For example, a news box could slide open if you click a certain link.

Problems can often occur if a user is a bit keen on clicking, or impatient, and ends up clicking the link repeatedly, before the animation has taken place. Browsers can often 'queue' a backlog of clicks so that things keep happening long after they should have ended! This can also cause unexpected behaviour which can make the user think that the website is 'broken'.

But fear not, there is a simple solution:


At the start of any click event in jquery, which may look like this:

$('.element').click(function() { ....

Simply add the following:

$('.animatedelement').is(":animated")){
           return;
  }

This will check if an animation is running, and if it is, put the brakes on any other events until it is finished.

Hey presto!


Leave a Comment back to blog