How to prevent one click function to be called within two click function?

jquery disable click event temporarily
jquery ::after click event
jquery on click
jquery after click event finish
disable click event jquery
jquery event handler
jquery off not working
one click function

I am trying to add custom behaviour to my buttons. If clicked once - one action is performed. If clicked twice another one is performed.

I saw such a solution in this answer and I have tried all possible combinations:

        clickOnce.shouldBeRequiredToFailByGestureRecognizer(clickTwice)
        clickTwice.shouldBeRequiredToFailByGestureRecognizer(clickOnce)
        clickOnce.shouldRequireFailureOfGestureRecognizer(clickTwice)
        clickTwice.shouldRequireFailureOfGestureRecognizer(clickOnce)
        clickOnce.canPreventGestureRecognizer(clickTwice)
        clickOnce.canBePreventedByGestureRecognizer(clickTwice)
        clickTwice.canPreventGestureRecognizer(clickOnce)
        clickTwice.canBePreventedByGestureRecognizer(clickOnce)

But nothing worked for me.

And here's the full code:

import Cocoa

class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let buttonTestOne = NSButton(frame: CGRect(x: 500, y: 500, width: 100, height: 500))

        let clickOnceForTestOne = NSClickGestureRecognizer(target: buttonTestOne, action: #selector(ViewController.clickOneTime(_:)))
        clickOnceForTestOne.numberOfClicksRequired = 1
        buttonTestOne.addGestureRecognizer(clickOnceForTestOne)
        self.view.addSubview(buttonTestOne)

        let arrTestButtons = ["One", "Two", "Three"]

        var x = 0
        var y = 0

        for item in arrTestButtons{
            let buttonNew = NSButton(frame: CGRect(x: x, y: y, width: 100, height: 100))
            x = x + 120
            y = y + 120
            buttonNew.attributedTitle = NSAttributedString(string: item)
            let clickOnce = NSClickGestureRecognizer(target: self, action: #selector(ViewController.clickOneTime(_:)))
            clickOnce.numberOfClicksRequired = 1
            buttonNew.addGestureRecognizer(clickOnce)
            let clickTwice = NSClickGestureRecognizer(target: self, action: #selector(ViewController.clickTwoTimes(_:)))
            clickTwice.numberOfClicksRequired = 2
            buttonNew.addGestureRecognizer(clickTwice)
            clickOnce.shouldBeRequiredToFailByGestureRecognizer(clickTwice)
            clickTwice.shouldBeRequiredToFailByGestureRecognizer(clickOnce)
            clickOnce.shouldRequireFailureOfGestureRecognizer(clickTwice)
            clickTwice.shouldRequireFailureOfGestureRecognizer(clickOnce)
            clickOnce.canPreventGestureRecognizer(clickTwice)
            clickOnce.canBePreventedByGestureRecognizer(clickTwice)
            clickTwice.canPreventGestureRecognizer(clickOnce)
            clickTwice.canBePreventedByGestureRecognizer(clickOnce)

            self.view.addSubview(buttonNew)
        }


    }

    func clickOneTime(g:NSClickGestureRecognizer){
        if g.state == .Ended {
            Swift.print("single click")
        }
    }
    func clickTwoTimes(g: NSClickGestureRecognizer){
        if g.state == .Ended {
            Swift.print("DOUBLE CLICK!")
        }
    }
    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

What am I doing wrong? I think the mistake must be pretty simple, I am either calling from the wrong place, or somewhat like that, but I can't understand.

Here is the log file, which is the same for all combinations:

Clicked once:

single click

As expected

Clicked twice:

single click
DOUBLE CLICK!

Which starts both single and double click.

I have read the documentation. And I've tried all the combinations, because couldn't find a solution.

I have also tried with the logs. So Swift.print(clickOnce.shouldBeRequiredToFailByGestureRecognizer(clickTwice)) and one of these, but it gives me false.

Try this:

let click = NSClickGestureRecognizer(target: self, action: #selector(clicked(_:))
click.numberOfClicksRequired = 2

jQuery off() Method, Remove the click event for all <p> elements: As of jQuery version 1.7, the off() method is the new replacement for the unbind(), die() and Tip: To attach an event that only runs once and then removes itself, use the one() method. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot  We'll use fictitious function names One(), Two(), and Three() to show how to implement the onclick attribute for calling multiple functions. There are two ways to do many things with one click: Put all the function calls into the onclick attribute's value.

I was trying to solve this myself; it turns out that the solution is relatively straightforward.

Use a gesture recogniser for the double-click (here, I've added them to the view, but the same principle should work for buttons)

let doubleClickRecognizer = NSClickGestureRecognizer(target: self, action: #selector(ViewController.clickTwice))
doubleClickRecognizer.numberOfClicksRequired = 2
self.view?.addGestureRecognizer(doubleClickRecognizer)

Add

@objc func clickTwice() {
    print("double click")
}

and then handle single clicks through

override func mouseDown(with event: NSEvent) {
    print("mouse down; once")
}

(or appropriate equivalent).

NSGestureRecognizer has

var delaysPrimaryMouseButtonEvents: Bool

which by default is set to true, so with this combination, it waits until the doubleClickRecognizer has handled the double-click before going up the responder chain to see who else is interested in a mouseclick.

Two gesture recognisers have the same problem as handling single and double clicks in mouseDown: the single click fires first. In order to overcome this, you need to override

func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool  

This is not a function you call, it's a delegate function that you override. So you set your viewController to conform to NSGestureRecognizerDelegate (which means it will receive delegate methods); set singleClickRecognizer.delegate = self (where self is your viewController), and implement

func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
    if gestureRecognizer == singleClickRecognizer && otherGestureRecognizer == doubleClickRecognizer {
        return true
    }
    return false
    }

which states that your singleClickRecognizer waits for the doubleClickRecognizer to fail before executing its selector.

Unfortunately, I have found that in SpriteKit, while this code does what it says on the tin (process a double-click first), it leads to unacceptable delays, so I'm not sure I can recommend it.

jQuery Event Methods, Event methods trigger or attach a function to an event handler for the selected elements. The following dblclick(), Attaches/Triggers the double click event event.stopImmediatePropagation(), Prevents other event handlers from being called. Click on a <button> element to display the current day, date and time: <button onclick="getElementById('demo').innerHTML = Date()">What is the time?</button> Try it Yourself »

Maybe you could try it this way, where "button" is your button

let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.clickTwoTimes(_:)))
let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.clickOneTime(_:)))
singleTapRecognizer.numberOfTapsRequired = 1
doubleTapRecognizer.numberOfTapsRequired = 2
button.addGestureRecognizer(doubleTapRecognizer)
button.addGestureRecognizer(singleTapRecognizer)
singleTapRecognizer.requireGestureRecognizerToFail(doubleTapRecognizer)

event.preventDefault(), For example, clicked anchors will not take the browser to a new URL. We can use event.isDefaultPrevented() to determine if this method has been called by an event handler that 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23 <a href="https://jquery.com">default click action is prevented</a>​. JavaScript - Disable Button after Click using JavaScript Function In this code snippet we will learn how to disable a button on click event. In this example there will be button and it will be disabled after click on the button.

.one(), A string containing one or more JavaScript event types, such as "click" or "submit,​" or and the values represent a handler function to be called for the event(s). In the example above the alert could be displayed twice due to the two event  Check the option for Single click to open an item to enable single-click to open items. Check Double-click to open item to disable the single-click feature. Filed Under: Windows Tagged With: win7, Windows 10, Windows 8. Reader Interactions. This site uses Akismet to reduce spam. Learn how your comment data is processed.

Element: click event, An element receives a click event when a pointing device button (such as a mouse's primary mouse button) In other words, detail will be 2 for a double-​click, 3 for triple-click, and so forth. Stop using click event delegation. JavaScript event, or trigger that event on an element. Type: Function( Event eventObject ) A function to execute each time the event is triggered. An object containing data that will be passed to the event handler. Type: Function( Event eventObject ) A function to execute each time the event is triggered.

The difference between 'return false;' and 'e.preventDefault();', Have you ever seen those two things (in the title) being used in jQuery? Here is a simple example: $("a").click(function() { The return false; part of that code prevents the browser from performing the default action for that link. an event happens on an element, that event is triggered on every single parent element as well. Multiple Function Calls with One Onclick Attribute. This question comes up from time to time. And the methods I've seen on the Internet are a bit short of complete. The question is, "How do I run more than one JavaScript function with one onclick= or onsubmit= call?" and variations thereof.

Comments
  • You cannot simply mash all kinds of lines of cod together and expect the code to run as expected. Read the docs for each of the methods you call and try to figure out which gesture recognizer can influence the other in what way.
  • The example you referred to is for iOS, not OS X, so clicking twice with a mouse probably won't have the same effect as double tapping with a finger on a device. Also "nothing worked for me" gives no indication of how it's not working, and you should explain that in your question.
  • @luk2302 I didn't mash them all, I've just tried all the combinations and posted code like that for the brevity
  • I doubt you tried all of them, there are 2^8, aka 256 combinations. Please take my advice and read the documentation.
  • @luk2302 I have read the documentation, OK I haven't tried all the 256 combos, but I've tried what seemed to be the most logical from the documentation. Each one-by-one and altogether. At least if there could be a combination to work it would appear.
  • This is iOS and I am trying to find the solution for OS X.