2016-12-06 3 views
-1

Ich habe 2 JavaScript-Funktion, die als Objekteigenschaft verwendet werden. Ich möchte zwei Funktionen gleichzeitig ausführen. Ich habe sie an planogram Funktion angeschlossen, aber immer noch nicht funktioniert und geben Fehler. Hier ist mein Code.Wie führe ich 2 JavaScript-Funktionen gleichzeitig aus?

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

Ich habe auch versucht stayInGroup Funktion innerhalb avoidNodeOverlap Funktion aufzurufen, aber stayInGroup Funktion ignoriert. Wenn jemand eine Idee hat zu helfen, schätze ich es sehr. Entschuldigung für meine schlechte Grammatik, danke

+0

@Satpal warum würde es parallel ausführen? – Mahi

+0

@Satpal noch nicht funktioniert –

Antwort

0

Möchten Sie diese 2 Funktionen in sequentieller Reihenfolge ausführen ODER möchten Sie diese parallel ausführen?

JavaScript wird asynchron ausgeführt. In dem in Ihrer Nachricht angegebenen Codebeispiel werden die Funktionen daher parallel/asynchron aufgerufen.

Wenn Sie sie in linearer sequentieller Reihenfolge ausführen möchten, müssen Sie die Funktionsrückrufe erstellen und die 2. Funktion im Callback-Handler der 1. Funktion ausführen.

+0

würden Sie mir ein paar Beispiele geben? Ich kann beides verwenden (parallel oder linear). Ich will nur, dass meine Funktion funktioniert –

+0

Von Ihrem Code 'Funktion Planogramm (Knoten, pt, gridpt) { avoidNodeOverlap (Knoten, pt, gridpt); stayInGroup (Knoten, Punkt, gridpt); } ' Die 2 Funktionen werden asynchron aufgerufen. Wenn man sich den Code ansieht, ist der einzige Grund dafür, dass 'stayInGroup' nicht das erwartete Ergebnis liefert, die 'if'-Bedingung in Zeile Nr. 3 Ihrer Methode 'stayInGroup'. Haben Sie den übergebenen Wert verifiziert, um sicherzustellen, dass der Methodenaufruf nicht vorzeitig aus der Zeile Nr. 2 ausgeführt wird? 3? Können Sie auch weitere Details zum Fehler angeben, wenn ein Fehler auftritt? –

+0

der Fehler ist 'Kann Eigenschaft 'x' von undefined nicht lesen. Innerhalb der 'stayInGroup' Funktion gebe ich eine Warnung nach 'if (grp === null) return pt;' line, aber meine Warnung wird nicht ausgeführt. Wenn ich vor 'var grp = part.containingGroup' eine Warnung gebe, funktioniert es. Hinweis: dass Funktionen tatsächlich funktioniert. aber wenn ich mich ihnen anschließe, bekomme ich Ärger. –

Verwandte Themen