Javascript函数存储在变量error中

Javascript函数存储在变量error中,javascript,Javascript,有什么原因会让我出错吗 未捕获类型错误:inter不是函数 在movingPieceScript.js:270 在keyboardMove script.js:146 在此代码中: var inter = setInterval(function() { draw(); b_ctx.globalCompositeOperation = "copy"; b_ctx.fillStyle = "purple"; b_ctx.beginPath(); b_ctx.arc(xcoord

有什么原因会让我出错吗

未捕获类型错误:inter不是函数 在movingPieceScript.js:270 在keyboardMove script.js:146

在此代码中:

var inter = setInterval(function() {
  draw();
  b_ctx.globalCompositeOperation = "copy";
  b_ctx.fillStyle = "purple";
  b_ctx.beginPath();
  b_ctx.arc(xcoord, y, 45, 0, Math.PI * 2, true);
  b_ctx.fill();
  y += 1;
  if (y > endY) clearInterval(inter)
}, 25);

inter();

国际米兰并不是一个真正的功能。所以你不能叫它。它每25毫秒自动调用一次

您得到错误是因为您正在调用它。它被自动调用。您不需要调用它。

setInterval不返回函数。像这样修改它

   var inter = function(){
                    var interVar = setInterval(function(){
                    draw();
                    b_ctx.globalCompositeOperation="copy";
                    b_ctx.fillStyle = "purple";
                    b_ctx.beginPath();
                    b_ctx.arc(xcoord, y, 45, 0, Math.PI*2, true);
                    b_ctx.fill();
                    y+=1;
                    if(y>endY) clearInterval(interVar)}, 25);
               }
然后您可以运行inter并让它运行setInterval

尝试以下操作:

var inter = function(){
  var x= setInterval(function(){
                draw();
                b_ctx.globalCompositeOperation="copy";
                b_ctx.fillStyle = "purple";
                b_ctx.beginPath();
                b_ctx.arc(xcoord, y, 45, 0, Math.PI*2, true);
                b_ctx.fill();
                y+=1;
                if(y>endY) clearInterval(x)}, 25);
}
inter();

setInterval不返回函数?!放下国米;语句和您的代码应该可以工作。你为什么认为你需要它,你期望它做什么?它基本上是一个圆形垂直下降的动画。我不确定是否需要在末尾调用inter我猜他希望在以后调用它。这将引发一个错误,因为您无法在inter上运行clearInterval,这是容器函数。您好,我一直在使用此代码,但出于某种原因,y变量没有递增。在您的代码之前,我已经将变量设置为y=55,它只是停留在55,控制台也不会打印任何错误。我在if语句之前放了一个alertboom,但它没有执行。它只是一直进行到最后,然后返回到调用函数传递y变量作为函数参数,然后调用intery?可能是一个范围问题。