使用一次后,“ Uncaught TypeError:<函数名=“”>不是函数”

时间:2019-02-24 14:08:59

标签: javascript

我有一个功能,可以使div-Box“跳转”。该函数第一次运行,但是在使用一次之后,出现错误"Uncaught TypeError: jump is not a function"。有人可以解释为什么它不起作用吗?

already = false;

function jump() {
  if (already == false) { //So he can't jump 2 times in a row
    try {
      clearInterval(t); //<--this is my gravity function, where the div-Box falls down until it hits solid ground
    } catch (err) {
      console.log("not activated");
    }
    jump = setInterval("jump2();", 200);
    already = true;
  }
}
}
anz = 0;

function jump2() {
  //Getting coordinates of div-Boy(they work)
  var step = 10;
  var bottom = getBottom("itBoy");
  var right = getRight("itBoy");
  var top = getTop("itBoy");
  var left = getLeft("itBoy");
  //lets see if he hits an object
  if (anz <= 100) { //<-- anz = so he cant jump higher than 100 px
    if (top - step >= 0) {
      var a = hittest("itBoy", "up", 10); //if div wont hit a solid object --> return -1 | else return coordinates of bordes which collide (this function works too)
      if (a == -1) {
        anz += step;
        document.getElementById("itBoy").style.top = (top -= step) + "px";
      } else {
        document.getElementById(itBoy).style.top = a + "px";
        clearInterval(jump); // div stops moving upwards
        t = setInterval("move('down');", 50); //gravity gets Activated again
      }
    }
  } else {
    clearInterval(jump);
    t = setInterval("move('down');", 50);
  }
}

1 个答案:

答案 0 :(得分:1)

这是因为,您要覆盖跳转:

function jump(){
 // ...
 jump = setInterval("jump2();",200);
 // ^^ give it a different name

此外,在setInterval内部使用函数的一种好方法是:

setInterval(jump2, 200); // faster