Javascript 使用setTimeout动态更改svg元素的属性不会';I don’我没有按预期工作
我试图通过递归调用Javascript 使用setTimeout动态更改svg元素的属性不会';I don’我没有按预期工作,javascript,svg,Javascript,Svg,我试图通过递归调用moveSelected函数,将SVG移动到X,Y。moveSelected功能依次将新的x和y设置得稍微靠近x,y,以再现动画效果 以下是我的功能: this.moveSelected = function moveSelected(moveToHex) { //setting new cords on screen var self=this; var repeater; var curX = parseInt(this.selectedE
moveSelected
函数,将SVG移动到X,Y。moveSelected
功能依次将新的x和y设置得稍微靠近x,y,以再现动画效果
以下是我的功能:
this.moveSelected = function moveSelected(moveToHex) {
//setting new cords on screen
var self=this;
var repeater;
var curX = parseInt(this.selectedEle.getAttribute('cx'));
var curY = parseInt(this.selectedEle.getAttribute('cy'));
//console.log('curX '+curX+'curY ',curY);
if(curX < moveToHex.cx) //moveRight
{
this.selectedEle.setAttribute('cx', curX + .5);
}
if(curX > moveToHex.cx) //moveLeft
{
this.selectedEle.setAttribute('cx',curX-.5);
}
if(curY < moveToHex.cy) //movedown
{
console.log('move down');
this.selectedEle.setAttribute('cy', curY + .5);
}
if(curY > moveToHex.cy) //moveup
{
console.log('move Up');
this.selectedEle.setAttribute('cy', curY - .5);
}
console.log('condition:' + curY + '<' + parseInt(moveToHex.cy));
if((Math.abs(curY - moveToHex.cy)) < 1 &&
(Math.abs(curX - moveToHex.cx) < 1)) {
clearTimeout(repeater);
unitArray[this.selectedUnit.num].hexagon = moveToHex;
console.log('cleared');
}
else if(!repeater) {
repeater = setTimeout(function() {
self.moveSelected(moveToHex)
}, 25);
}
}
this.moveSelected=函数moveSelected(moveToHex){
//在屏幕上设置新跳线
var self=这个;
var中继器;
var curX=parseInt(this.selectedEle.getAttribute('cx');
var curY=parseInt(this.selectedEle.getAttribute('cy');
//console.log('curX'+curX+'curY',curY);
if(curXmoveToHex.cx)//moveLeft
{
this.selectedEle.setAttribute('cx',curX-.5);
}
if(curYmoveToHex.cy)//moveup
{
console.log('move Up');
this.selectedEle.setAttribute('cy',curY-.5);
}
console.log('condition:'+curY+'我不明白它为什么会这样做
…像什么?别让我们猜我的意思是为什么减法工作时它不会在curX或curY上加0.5这就像拔牙一样…它会做什么?是的,因为你解析它的行为是这样的-不要解析Damm!那很愚蠢,我很尴尬谢谢,谢谢