0

I have a onMouseDownEssence() and onMouseUpEssence() function for an HTML element, how to check if onMouseDownEssence() is called every time before calling onMouseUpEssence() to ensure I get the correct mouse down position?

Here is mousedown function:

var mouseDownIndex = -1;

function onMouseDownEssence(downIndex, e, className) {

    dragTarget = e.target;
    holdStarter = new Date().valueOf();

    mouseDownIndex = downIndex;
}

Here is mouseup function:

function onMouseUpEssence(upIndex, e, className) {

    var el = e.target;
    var holdActive = (new Date().valueOf() - holdStarter) > holdDelay;

    if (holdActive) {
        var thisUpTargetIndex = el.getAttribute("name");

        if (lastUpTargetIndex != null && thisUpTargetIndex != lastUpTargetIndex) {
            // console.log("double drag done");
            el.removeAttribute(dbl);
            lastUpTargetIndex = null;

            var selectedText = clickDragAutoExpand(mouseDownIndex, upIndex,
                    className);

        } else {
            // console.log("drag done");
            var selectedText = clickDragAutoExpand(mouseDownIndex, upIndex,
                    className);
        }

        holdActive = false;
    } else if (el.getAttribute(dbl) == null) {
        el.setAttribute(dbl, 1);
        setTimeout(
                function() {
                    if (el.getAttribute(dbl) == 1 && !dragTarget) {
                        if (e.button === 0) {
                            // console.log("single clicked ");
                            el.removeAttribute(dbl);

                            var selectedText = clickAutoExpand(upIndex,
                                    className);

                        }
                    } else {
                        if (el.getAttribute(dbl) != null)
                            lastUpTargetIndex = el.getAttribute("name");
                    }
                }, dblDelay);
    } else {
        // console.log("double clicked");
        el.removeAttribute(dbl);

        var selectedText = clickAutoExpand(upIndex, className);
    }

    dragTarget = null;

}
2
  • 4
    Please, include some of your code in order for us to help you. Commented Oct 15, 2018 at 14:02
  • Also explain what you are trying to do. Commented Oct 15, 2018 at 14:03

1 Answer 1

2

My approach would be to keep a track of whether mouseDownEssence() was called. And if not, call it before proceeding further. This approach would work somewhat as below. It would work differently for asynchronous functions but mouseDownEssence() seems to be a synchronous function.

let isMouseDownEssenceCalled = false;

function mouseDownEssence() {
  isMouseDownEssenceCalled = true;

  ...
}

function mouseUpEssence() {
  if (!isMouseDownEssenceCalled) {
    mouseDownEssence()
  }

  ...

  isMouseDownEssenceCalled = false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How to check if a function has been called AFTER executing another function everytime? Thanks.
The same principle applies. Use variables to keep track of function execution. Of course, as mentioned previously, different approach should be used for an asynchronous function.

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.