2

Is it possible to create custom events (for example, triple click or swipe) in JavaScript that will trigger with certain conditions? Perhaps to listen to this custom event:

addEventListener('trplclick', handler, false); 

So handler will be called only if 3 clicks were done within 600 ms, for example.

7
  • from client side to server Side ? Commented Mar 27, 2013 at 18:32
  • what from client side to server side? Commented Mar 27, 2013 at 18:33
  • @AkshayJoy he tagged this with javascript? And the code is js. This is client side? Commented Mar 27, 2013 at 18:33
  • you need createevent Handler for Server side event from javascript.? Commented Mar 27, 2013 at 18:34
  • 1
    @AkshayJoy that statement doesn't even make sense. Commented Mar 27, 2013 at 18:34

2 Answers 2

1

You need to trigger the triplclick event yourself. Listen to the individual click events, count them, and trigger triplclick when it makes sense.

Have a look at document.dispatchEvent.

I'm using jQuery to demonstrate the concept, but you don't need to:

var count = 0;
var resetTimeout - 1;
$(element).on("click", function(){
    count++;
    clearTimeout(resetTimeout);
    if (count === 3)
    {
        count = 0;
        $(element).trigger("trplclick");
    }
    else 
    {
        resetTimeout = setTimeout(function(){
            count = 0; // Don't count if no click within the last second.
        }, 1000);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

See similar question here: How do I listen for triple clicks in JavaScript?

Basically what it does is combining double click event and single click event together to mimic triple click. When double click event triggers, set a timeout for 200ms. If there's a following click coming up, check if the timer timed out. If this click happens 200ms later after the previous double click(at this time, timer was set to null in double click handler), discard this click, otherwise count it as a triple click.

For setting custom event in general, I will refer you to Nicholas C. Zakas's blog post: http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.