TunesHub

The Great Hub Of Technology Tunes


Like TunesHub's facebook page to get working tricks.

Use click and doubleClick on same button using jQuery

Published on: at 8:17 PM | Posted By:
You can use following function in your webpage to use click and doubleClick using same button in jQuery. Normally if you use click and double click listener you will see only click is being initiated not double click.
     
$(document).ready(function () {

var DELAY = 700, clicks = 0, timer = null;

$(function () {

$("#myButton").on("click", function (e) {

clicks++;  //count clicks

if (clicks === 1) {

timer = setTimeout(function () {

// DO click operations
clicks = 0;             //after action performed, reset counter

}, DELAY);

} else {

clearTimeout(timer);    //prevent single-click action
// DO double click operations
clicks = 0;             //after action performed, reset counter
}

})
.on("dblclick", function (e) {
e.preventDefault();  //cancel system double-click event
});

});

});

1 comment On "Use click and doubleClick on same button using jQuery"

  1. Optimizing user interactions in gaming interfaces is crucial, and this jQuery code snippet beautifully demonstrates how to handle both single clicks and double clicks on a button. In gaming mini militia mod by vishnu scenarios, where precision and timing matter, incorporating such techniques can enhance player experience, ensuring that both regular and double-click actions are seamlessly executed, providing a more responsive and engaging gameplay environment."





    ReplyDelete

Please try to write English. Don't use Bangla, Banglish etc. And if you post a comment in other languages then it may got deleted or ignored without notice.