javascript - Defining value that doesnt change -


i realise title might misleading, did not know how phrase other way.

the problem im facing draw svg panel json object, , in object there moving parts being controlled server sending values us. moving part in right position, after awhile keeps adding x position goes out of border, not meant do. add x+x+x repeatedly initialposition, want consider initialposition first when calculating new position at.

here function behind that, tried having if statement checked variable standardx , standardy, while set undefined set obj.transform.baseval.getitem(length).matrix.e value it, did not work.

any tips on workaround? in advance.

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

not sure don't have access whole code.
gather problem you're having initializing standardx , standardy @ every call of function.

you might want initialize them when declare function, :

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; 

another option might following, if needs kind of dynamic initialization.

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

to noted of possible because can access variables declared outside current function.
can find more on function scope , context switch in this article.


Comments