Javascript 如何在每次执行另一个函数之前检查函数是否已被调用
我有一个HTML元素的onMouseDownEssential()和onMouseUpEssential()函数,如何在每次调用onMouseUpEssential()之前检查是否调用了onMouseDownEssential(),以确保获得正确的鼠标下键位置 下面是mousedown函数:Javascript 如何在每次执行另一个函数之前检查函数是否已被调用,javascript,Javascript,我有一个HTML元素的onMouseDownEssential()和onMouseUpEssential()函数,如何在每次调用onMouseUpEssential()之前检查是否调用了onMouseDownEssential(),以确保获得正确的鼠标下键位置 下面是mousedown函数: var mouseDownIndex = -1; function onMouseDownEssence(downIndex, e, className) { dragTarget = e.ta
var mouseDownIndex = -1;
function onMouseDownEssence(downIndex, e, className) {
dragTarget = e.target;
holdStarter = new Date().valueOf();
mouseDownIndex = downIndex;
}
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;
}
下面是mouseup函数:
var mouseDownIndex = -1;
function onMouseDownEssence(downIndex, e, className) {
dragTarget = e.target;
holdStarter = new Date().valueOf();
mouseDownIndex = downIndex;
}
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;
}
我的方法是跟踪是否调用了
mouseDownEssence()
。如果没有,请在继续之前致电。这种方法的工作原理如下。对于异步函数,它的工作方式会有所不同,但是mousedownesse()
似乎是一个同步函数
let isMouseDownEssenceCalled = false;
function mouseDownEssence() {
isMouseDownEssenceCalled = true;
...
}
function mouseUpEssence() {
if (!isMouseDownEssenceCalled) {
mouseDownEssence()
}
...
isMouseDownEssenceCalled = false;
}
我的方法是跟踪是否调用了
mouseDownEssence()
。如果没有,请在继续之前致电。这种方法的工作原理如下。对于异步函数,它的工作方式会有所不同,但是mousedownesse()
似乎是一个同步函数
let isMouseDownEssenceCalled = false;
function mouseDownEssence() {
isMouseDownEssenceCalled = true;
...
}
function mouseUpEssence() {
if (!isMouseDownEssenceCalled) {
mouseDownEssence()
}
...
isMouseDownEssenceCalled = false;
}
请包含一些代码,以便我们帮助您。还请解释您尝试执行的操作。请包含一些代码,以便我们帮助您。还请解释您尝试执行的操作。如何检查每次执行另一个函数后是否调用了某个函数?谢谢。同样的原则也适用。使用变量跟踪函数的执行。当然,正如前面提到的,异步函数应该使用不同的方法。如何检查每次执行另一个函数后是否调用了一个函数?谢谢。同样的原则也适用。使用变量跟踪函数的执行。当然,正如前面提到的,异步函数应该使用不同的方法。