循环执行if和else

时间:2018-09-21 13:44:52

标签: javascript angularjs typescript loops

嗨,我有两个函数:第一个函数检查条件是否为真,它返回一个变量。该变量在第二个函数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,
        });
    }
}

发生的事情是,当我使用断点时,如果加上delta,则abc()中的条件会通过,然后直接进入else条件。因此最终值始终根据其他条件打印。

我希望仅当MapHeightCollision()返回一些值的条件值时才打印。

1 个答案:

答案 0 :(得分:0)

总而言之,您需要更改MapHeightCollision函数,因为1.您永远不会返回假值,并且2.您在同一块中两次使用return,因此永远不会达到第二次返回。

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

  }

  return false;
}