簡體   English   中英

循環執行if和else

[英]Loop executing both if and else

嗨,我有兩個函數第一個檢查條件是否正確,它返回一個變量。 該變量在第二個函數if語句中使用。 像這樣

 private MapHeightCollision(feature: any) { const mapElementHeight = $(this.gisMapElement).height(); let spiralcounter = UtilityConst.SPIRAL_COUNTER; const height = UtilityConst.INITIAL_BUBBLE_HEIGHT; let rotation = 0; const plotxy: any = this.markerSpiral(spiralcounter, feature, rotation); let delta; if (plotxy.y > 0 || (plotxy.y + height) > mapElementHeight) { delta = (plotxy.y + height) - mapElementHeight; console.log("collide") } return delta; return true; } private abc() { if (this.MapHeightCollision(feature)) { const y = plots.y; const delta = this.MapHeightCollision(feature) $(markerBubble).css({ top: y + delta, left: plots.x, }); } else { $(markerBubble).css({ top: plots.y, left: plots.x, }); } } 

發生的是,當我使用斷點時,如果添加了增量,則abc()中的條件就會通過,然后直接進入其他條件。 因此最終值始終根據else條件打印。

我希望僅當MapHeightCollision()返回某個值的條件值時才打印它。

總結起來,您需要更改MapHeightCollision函數,因為1.您永遠不會返回假值,並且2.您在同一塊中兩次使用return,所以永遠不會達到第二次返回。

  if (plotxy.y > 0 || (plotxy.y + height) > mapElementHeight) {
      delta = (plotxy.y + height) - mapElementHeight;
      console.log("collide")
      return delta;

  }

  return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM