是否可以用一个替换两个相反和不同的功能?

问题描述:

我想优化和减少我的代码,以提高它的性能和正确性。通过下面的这两个不同的功能,我可以成功地在地图上使用pathIndex向前和向后移动Google Map Marker,在GPS坐标数组上计算出来[我没有包括这部分代码,因为我认为它不是这个问题,而是我可以并且会在需要时发布]。是否可以用一个替换两个相反和不同的功能?

这是我的代码:

1功能

function animate() { 
    if (pathIndex < coord.length && mapAnimationStatus != PLAY_PAUSED) { 
     googleMapsMarker.setPosition(coord[pathIndex]); 
     googleMap.panTo(coord[pathIndex]); 

     pathIndex += 1; 

     if (pathIndex == coord.length) { 
      pause(); 

      pathIndex = 0; 
      mapAnimationStatus = NOT_PLAY; 

      return; 
     } 

     timerHandler = setTimeout("animate(" + pathIndex + ")", 1000); 
    } 
} 

第2个功能

function animateRewind() { 
    if (pathIndex >= 0 && mapAnimationStatus != PLAY_PAUSED) { 
     googleMap.panTo(coord[pathIndex]); 
     googleMapsMarker.setPosition(coord[pathIndex]); 

     if (pathIndex == 0) { 
      pause(); 

      mapAnimationStatus = NOT_PLAY; 

      return; 
     } 

     pathIndex -= 1; 

     timerHandler = setTimeout("animateRewind(" + pathIndex + ")", 1000); 
    } 
} 

正如你可以看到这两个函数共享了很多的部分代码,它认为它们可以替换为一个单一的原因,但我不能figur如何做到这一点。

那么,是否可以创建一个单独的function来管理这两个不同的动画?

+0

你能交换'panTo'和'setPosition'电话?你可以用'pause'调用交换'pathIndex = 0;'吗?如果是这样,我会有一个相当简单的解决方案 – Bergi 2014-10-31 14:53:39

+0

为什么'setTimout'中的'animate'和'animateRewind'调用需要一个参数? – Bergi 2014-10-31 14:54:47

+0

@Bergi你是什么意思的“交换pathIndex”? – Aluminum 2014-10-31 15:01:01

我希望我没有错过什么...

function animate(pathIndex, dir) { 
    var animateDir = (pathIndex < coord.length 
     && mapAnimationStatus != PLAY_PAUSED && dir == 'f') 
      ? dir 
      : (pathIndex >= 0 
      && mapAnimationStatus != PLAY_PAUSED && dir == 'r') 
      ? dir : "error"; 

    if (animateDir === "r") { googleMap.panTo(coord[pathIndex]); } 
    if (animateDir !== 'error') { googleMapsMarker.setPosition(coord[pathIndex]); } 
    if (animateDir === "f") { 
     googleMap.panTo(coord[pathIndex]); 
     pathIndex += 1; 
    } 

    if (animateDir !== 'error') { 
     if (pathIndex == coord.length || pathIndex == 0) { 
      pause(); 
      pathIndex = animateDir === "f" ? 0 : pathIndex; 
      mapAnimationStatus = NOT_PLAY; 
      return; 
     } 
     pathIndex = animateDir === "f" ? pathIndex - 1 : pathIndex; 
     timerHandler = setTimeout("animate(" + pathIndex + "," + animateDir + ")", 1000); 
    } 
} 
+0

这看起来比原来的两个功能复杂得多。 – Bergi 2014-10-31 14:52:17

+0

是的,它变得相当复杂,我不知道泛和顺位的问题,也是我对原作者包含的函数的参数感到困惑,哦... – Urielzen 2014-10-31 15:03:53

+0

是的,我要求澄清有关OP在尝试回答之前:-) – Bergi 2014-10-31 15:07:32

你可以试试这个:

function ConcatenateFunctions() { 
    if(mapAnimationStatus != PLAY_PAUSED){ 
     googleMap.panTo(coord[pathIndex]); 
     googleMapsMarker.setPosition(coord[pathIndex]); 

     if (pathIndex < coord.length) { 
      pathIndex += 1; 
      if (pathIndex == coord.length) { 
       pause(); 
       pathIndex = 0; 
       mapAnimationStatus = NOT_PLAY; 
       return; 
      } 
     }else if (pathIndex >= 0) { 
      if (pathIndex == 0) { 
       pause(); 
       mapAnimationStatus = NOT_PLAY; 
       return; 
      } 
      pathIndex -= 1; 
     } 

     timerHandler = setTimeout("ConcatenateFunctions(" + pathIndex + ")", 1000); 
    } 
} 

希望这将有助于!

+0

我没有让你失望,快速阅读看起来好像你已经回答了这个问题。但是,'setTimeout'参数应该是'ConcatenateFunctions'而不是'animate'。 (或者你可以将函数重命名为'animate')。另外,'pathIndex'应该作为参数添加到函数中,或者从'setTimeout'中移除。这也是OP代码的问题。 – 2014-10-31 14:56:40

+0

@RickHitchcock我的错误(动画)只是修复它!但从我从上面的代码中得到的结果,他以这种方式使用它们,我只是将它们连接起来工作......我不知道pathIndex来自哪里...... – 2014-10-31 15:01:04

+1

'pathIndex'要么是全局的变量或OP的函数在另一个函数内。把'pathIndex'留在'setTimeout'中是没问题的 - 它只是没有做任何事情。这相当于写'setTimeout(ConcatenateFunctions,1000)'。 – 2014-10-31 15:04:07