Take the 2-minute tour ×

I have code in jQuery like this:


and inside function I'd like to check if #something was clicked only once at a short time and if twice and more, then act like once.

I tried this code:

if(!event.detail || event.detail == 1) { do_something; }

and it works in most browsers except Firefox. I know why, but didn't find any solution for this. I tried preventDefault on dblclick event, but it doesn't work in Chrome...

Any ideas?

share|improve this question
If you want to fire the event only once. There's a jQuery method .one() for that –  Jashwant Mar 15 '13 at 12:44
I don't want to fire it only once, but once at a short time. If triggered event is completed, then I can trigger it again, but not until it's finished. So, basically, I want to ignore triggering the same event if it's being processed, but after it's done, I want to be able to trigger it again. –  Ks Ldsad Mar 15 '13 at 12:53
You can disable to button which triggers the event until the processing is finished. –  Jashwant Mar 15 '13 at 12:58
Ok, but how to check if processing is finished? –  Ks Ldsad Mar 15 '13 at 13:21
Can you show the code inside click function ? –  Jashwant Mar 15 '13 at 13:24
show 1 more comment

4 Answers

There is rogue way of doing it.

Have a global variable,

Inside the click event , increment it.

When enough clicks accumulated, remove the click event from the element using $(this) object

share|improve this answer
add comment

You can use the event.timeStamp to determine the time between two events.

$("#something").click(function( event ) {  
    this.lastClick = this.lastClick || 0;

    if( event.timeStamp - this.lastClick > 200 ) {
        // do something
    } else {
        // last click occured below 200ms, set out `lastClick` to the current timestamp

    this.lastClick = event.timeStamp;

I am slightly sloppy here, storing the lastClick on the DOM elements object representation. You might want to put that variable in a higher scope of your app.

share|improve this answer
Explain ??? event.timeStamp > 200 ??? –  madhairsilence Mar 15 '13 at 12:27
@madhairsilence: I had some errors in the code, updated. –  jAndy Mar 15 '13 at 12:38
@jAndy, is lastClick a valid property ? :O –  Jashwant Mar 15 '13 at 12:45
It won't work, because I don't know the exact amount of time. –  Ks Ldsad Mar 15 '13 at 12:54
@KsLdsad: well, yes its a valid property, we are creating it on our own there. In this example, its checking whether or not the last click on the specific element occured below 200ms. You can of course adapt that value to your needs. –  jAndy Mar 15 '13 at 13:06
add comment

I am modifying you code a bit,

var counter =0;
//alert("Clicked : " + counter);

I hope this is the simplest way to know how many times it clicked.

if want to control clicks, just check with if

if(counter >= 1) {...}

I think this will do :)

share|improve this answer
It's not that simple unfortunately. –  Ks Ldsad Mar 15 '13 at 12:56
add comment

Use .dblclick() (JQUERY) -- double click Use .click() (JQUERY) - single click

If 1,2 clicks is pretty enought for your task - use them.

Good luck.

share|improve this answer
Not an answer. Worth as a comment –  madhairsilence Mar 15 '13 at 12:42
It won't work this way. –  Ks Ldsad Mar 15 '13 at 12:59
add comment

Your Answer


