简体   繁体   中英

How to check if a function has been called before executing another function everytime

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;

}

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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM