定义不变的价值

时间:2015-07-17 08:49:17

标签: javascript

我意识到标题可能会产生误导,但不知道如何以任何其他方式表达它。

我面临的问题是我从JSON对象中绘制一个SVG面板,并且在该对象中有一些移动部件由正在向我们发送值的服务器控制。我让移动部件处于正确的位置,但过了一段时间它不断向自己添加X位置以至于它离开边界,这并不意味着要做。基本上它的作用是从初始位置重复添加X + X + X,但我希望它在计算新位置时首先考虑初始位置。

这是后面的函数,我尝试使用if语句检查变量standardX和standardY,而将它们设置为undefined,它将obj.transform.baseVal.getItem(length).matrix.e设置为对它有价值,但那不起作用。

任何解决方法的提示? 提前谢谢。

var moveX = function(elem, i) {
return function () {
    var stepvalue = 6.25;
    var step = "0";
    var x1 = "0";
    var y1 = "0";
    var obj = elem.dyn[i].obj;
    var standardX = obj.transform.baseVal.getItem(length).matrix.e;
    var standardY = obj.transform.baseVal.getItem(length).matrix.f;
    console.log(elem.value);

    if (elem.value <= elem.dyn[i].rlow) {
        step = 0;
        x1 = standardY;
        y1 = standardX;
        obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


    } else if (elem.value > elem.dyn[i].rlow && elem.value <= elem.dyn[i].rhigh) {
        step = stepvalue * elem.value;
        x1 = step -25;
        y1 = standardY;
        x1 += standardX;
        obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


    } else {
        step = stepvalue * elem.dyn[i].rhigh;
        x1 = step -25;
        y1 = standardY;
        x1 += standardX;
        obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");

    }

}};

1 个答案:

答案 0 :(得分:1)

不确定,因为我们无法访问整个代码 从我收集到的问题来看,你在每次调用函数时都在初始化standardX和standardY。

您可能希望在声明函数时初始化它们,如下所示:

var moveX = function(elem, i) {
    return function () {
        var stepvalue = 6.25;
        var step = "0";
        var x1 = "0";
        var y1 = "0";
        var obj = elem.dyn[i].obj;

        console.log(elem.value);

        if (elem.value <= elem.dyn[i].rlow) {
            step = 0;
            x1 = moveX.standardY;
            y1 = moveX.standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


        } else if (elem.value > elem.dyn[i].rlow && elem.value <= elem.dyn[i].rhigh) {
            step = stepvalue * elem.value;
            x1 = step -25;
            y1 = moveX.standardY;
            x1 += moveX.standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


        } else {
            step = stepvalue * elem.dyn[i].rhigh;
            x1 = step -25;
            y1 = moveX.standardY;
            x1 += moveX.standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");

        }

    };
};
moveX.standardX = obj.transform.baseVal.getItem(length).matrix.e;
moveX.standardY = obj.transform.baseVal.getItem(length).matrix.f;

如果它需要某种动态初始化,则可能是以下选项。

var moveX = function(elem, i) {
    var standardX = obj.transform.baseVal.getItem(length).matrix.e;
    var standardY = obj.transform.baseVal.getItem(length).matrix.f;

    return function () {
        var stepvalue = 6.25;
        var step = "0";
        var x1 = "0";
        var y1 = "0";
        var obj = elem.dyn[i].obj;

        console.log(elem.value);

        if (elem.value <= elem.dyn[i].rlow) {
            step = 0;
            x1 = standardY;
            y1 = standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


        } else if (elem.value > elem.dyn[i].rlow && elem.value <= elem.dyn[i].rhigh) {
            step = stepvalue * elem.value;
            x1 = step -25;
            y1 = standardY;
            x1 += standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");


        } else {
            step = stepvalue * elem.dyn[i].rhigh;
            x1 = step -25;
            y1 = standardY;
            x1 += standardX;
            obj.setAttribute("transform", "translate("+ x1 + "," + y1 +")");

        }

    };
};

需要注意的是,所有这些都是可能的,因为您可以访问在当前函数之外声明的变量 您可以在this article中找到有关函数范围和上下文切换的更多信息。