有没有可能用一个功能取代两个相反的不同功能

Is it possible to replace two opposite and different functions with a single one?

本文关键字:功能 两个 一个 有可能 取代      更新时间:2023-09-26

我想优化和减少我的代码,以提高它的性能和正确性。有了下面这两个不同的功能,我可以使用在GPS坐标阵列上计算的pathIndex成功地在地图上前后移动Google Map Marker[我没有包括这段代码,因为我认为它与这个问题无关,但如果需要,我可以也会发布它]。

这是我的代码:

第一个功能

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

第二个功能

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

正如你所看到的,这两个函数共享了很多代码部分,因此它认为可以用一个函数来替换它们,但我不知道如何做到这一点。

那么,有可能创建一个function来管理这两个不同的动画吗?

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

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

你可以试试这个:

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

希望它能有所帮助!