2014-10-31 27 views
0

我想優化和減少我的代碼,以提高它的性能和正確性。通過下面的這兩個不同的功能,我可以成功地在地圖上使用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

回答

0

我希望我沒有錯過什麼...

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

-1

你可以試試這個:

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

相關問題