I have code in jQuery like this:
$("#something").click(function() {
};
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?
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
alert('click');
} 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.
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
I am modifying you code a bit,
var counter =0;
$("#something").click(function() {
counter++;
//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 :)
Use .dblclick() (JQUERY) -- double click Use .click() (JQUERY) - single click
If 1,2 clicks is pretty enought for your task - use them.
Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.