我有以下功能:

div_paper.onmousedown = function(event) {
    var mouseCoords = getCoords(event);
    startX = mouseCoords.x - offset[0];
    startY = mouseCoords.y - offset[1];
    rect = paper.rect(startX, startY, 0, 0);
    document.onmousemove = doDraw;
    // the function below i would like to fire once
    rect.drag(drag_move, drag_start, drag_up);
};


如您所见,每次按下鼠标时都会触发。我想在第一次按下鼠标后仅触发一次drag函数。我应该为此使用jQuery .one()吗?

提前致谢。

最佳答案

袖手旁观,但是您可以尝试:

var dragged = false;
div_paper.onmousedown = function(event) {
    var mouseCoords = getCoords(event);
    startX = mouseCoords.x - offset[0];
    startY = mouseCoords.y - offset[1];
    rect = paper.rect(startX, startY, 0, 0);
    document.onmousemove = doDraw;
    // the function below i would like to fire once
    if (!dragged)
    {
       dragged = true;
       rect.drag(drag_move, drag_start, drag_up);
    }
};

关于javascript - 在其他函数中仅执行一次函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17221848/

10-09 14:25