Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在处理初始调用后的连续递归中“uncaughttypeerror:that.getAttribute不是函数”_Javascript_Recursion_This - Fatal编程技术网

Javascript 在处理初始调用后的连续递归中“uncaughttypeerror:that.getAttribute不是函数”

Javascript 在处理初始调用后的连续递归中“uncaughttypeerror:that.getAttribute不是函数”,javascript,recursion,this,Javascript,Recursion,This,我试图在用户单击屏幕上的对象时开始移动它,并在光标离开时阻止它移动 var sphere = d3.select('.sphere'); var trigger = 1; var speed = 0.5; sphere.on('click', click); sphere.on('mouseenter', mouseenter); sphere.on('mouseleave',mouseleave); function mouseenter(){ conso

我试图在用户单击屏幕上的对象时开始移动它,并在光标离开时阻止它移动

  var sphere = d3.select('.sphere');
  var trigger = 1;
  var speed = 0.5;
  sphere.on('click', click);
  sphere.on('mouseenter', mouseenter);
  sphere.on('mouseleave',mouseleave);

  function mouseenter(){
    console.log("Mouse Enter" + trigger)
    trigger = 1;
  }

  function mouseleave(){
    console.log("Mouse leave" + trigger)
    trigger = 0;
  }

  function click(){
    console.log("Mouse click" + trigger)
    var that = this;
    setTimeout(function(){
      console.log("MOVE");
      var currentPosition = that.getAttribute('position');
      var sphere = d3.select(that);
      console.log(currentPosition);
      currentPosition["z"] -= speed;
      console.log(currentPosition);
      sphere.attr('position', currentPosition);
      if(trigger){
        click();
      }
    }, 1000)
  }
结果

retry.html:41 Mouse click1
retry.html:44 MOVE
retry.html:47 {x: 0, y: 2.25, z: -1.5}
retry.html:49 {x: 0, y: 2.25, z: -2}
retry.html:41 Mouse click1
retry.html:44 MOVE
retry.html:45 Uncaught TypeError: that.getAttribute is not a function at retry.html:45
我希望它一直执行它,直到mouseleave执行。

使用Function.protytype.call显式定义函数执行的上下文:

if(trigger){
  click.call(that)
}