如何同时执行2个javascript函数?

时间:2016-12-06 06:29:10

标签: javascript

我有2个javascript函数将用作对象属性。我想同时运行两个函数。我已将它们加入到planogram函数中,但仍然没有工作并给出错误。这是我的代码。

    function planogram(node, pt, gridpt) {
        avoidNodeOverlap(node, pt, gridpt);
        stayInGroup(node, pt, gridpt);
    }

      // a Part.dragComputation function that prevents a Part from being dragged to overlap another Part
    function avoidNodeOverlap(node, pt, gridpt) {

        // this assumes each node is fully rectangular
        var bnds = node.actualBounds;
        var locate = node.location;

        // see if the area at the proposed location is unoccupied
        // use PT instead of GRIDPT if you want to ignore any grid snapping behavior
        var x = gridpt.x - (locate.x - bnds.x);
        var y = gridpt.y - (locate.y - bnds.y);
        var r = new go.Rect(x, y, bnds.width, bnds.height);

        // maybe inflate R if you want some space between the node and any other nodes
        if (isUnoccupied(r, node)) return pt;  // OK
        return locate;  // give up -- don't allow the node to be moved to the new location      
    }

    function stayInGroup(part, pt, gridpt) {
        // don't constrain top-level nodes
        var grp = part.containingGroup;
        if (grp === null) return pt;
        // try to stay within the background Shape of the Group
        var back = grp.findObject("rack");
        if (back === null) return pt;
        // allow dragging a Node out of a Group if the Shift key is down
        if (part.diagram.lastInput.shift) return pt;
        var p1 = back.getDocumentPoint(go.Spot.TopLeft);
        var p2 = back.getDocumentPoint(go.Spot.BottomRight);
        var b = part.actualBounds;
        var loc = part.location;
        // find the padding inside the group's placeholder that is around the member parts
        var m = (grp.placeholder !== null ? grp.placeholder.padding : new go.Margin(0));
        // now limit the location appropriately
        var x = Math.max(p1.x + m.left, Math.min(pt.x, p2.x - m.right - b.width - 1)) + (loc.x-b.x);
        var y = Math.max(p1.y + m.top, Math.min(pt.y, p2.y - m.bottom - b.height - 1)) + (loc.y-b.y);
        return new go.Point(x, y);
    }

我还试图在stayInGroup函数中调用avoidNodeOverlap函数,但忽略stayInGroup函数。如果有人有想法帮助我真的很感激。抱歉我的语法不好,谢谢

1 个答案:

答案 0 :(得分:0)

您想按顺序执行这两个功能还是想并行执行这些功能?

JavaScript以异步方式运行,因此在消息中发布的代码示例中,函数被称为parallels / asynchronously。

如果要以线性顺序执行它们,则需要创建函数回调并在第1个函数的回调处理程序中执行第2个函数。