在其他函数中只执行一次函数

时间:2013-06-20 19:10:45

标签: javascript jquery html

我有以下功能:

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);
};
正如你所看到的那样,每个mousedown都会发射。在第一个mousedown之后,我只想触发drag函数一次。我应该使用jQuery .one()吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

这不在袖口,但你可以尝试:

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