2016-12-06 53 views
-1

我有2个JavaScript函数将被用作对象属性。我想同时运行两个函数。我已经加入他们planogram功能,但仍然没有工作,并给出错误。这是我的代码。如何同时执行2个javascript函数?

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

我也试图调用内部avoidNodeOverlap功能stayInGroup功能,但stayInGroup功能被忽略。如果任何人有想法帮助,我真的很感激它。对不起,我的语法错误,谢谢

+0

@Satpal为什么会执行并行? – Mahi

+0

@Satpal仍然没有工作 –

回答

0

你想要按顺序执行这2个函数吗?或者你想并行执行这些?

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

如果您想要以线性顺序执行它们,您需要创建函数回调函数并在第一个函数的回调处理函数中执行第二个函数。

+0

你会给我几个样本?我可以同时使用(平行或线性)。我只想让我的函数起作用 –

+0

来自您的代码 'function planogram(node,pt,gridpt){ avoidNodeOverlap(node,pt,gridpt); stayInGroup(node,pt,gridpt); }' 这两个函数被称为异步。通过查看代码,'stayInGroup'不会产生预期结果的唯一原因是行号为'if'的条件。 3你的方法'stayInGroup'。您是否验证过要传递的值,以确保方法调用不会早于第no行。 3? 此外,如果有任何错误,您是否可以提供有关正在生成的错误的更多详细信息? –

+0

错误是'Can not read property'x'of undefined'。在'stayInGroup'函数内部,我在'if(grp === null)return pt;'line'之后给出警报,但是我的警报没有执行。如果我在'var grp = part.containingGroup;'之前发出警报,它就会起作用。 注意:该功能实际上工作。但如果我加入他们,我会遇到麻烦。 –