0

Is there any reason why I would be getting the error

Uncaught TypeError: inter is not a function at movingPiece (script.js:270) at keyboardMove (script.js:146)

in this code:

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();
3
  • setInterval does not return a function?! Just drop the inter(); statement and your code should work. Why did you think did you need it, what did you expect it to do? Commented May 14, 2017 at 10:59
  • It is basically an animation where a circle drops vertically. I wasn't sure if I needed to call inter() at the end Commented May 14, 2017 at 11:06
  • I am guessing he wants it called at a later period. Commented May 14, 2017 at 11:06

3 Answers 3

1

inter is not really a function. So you can't call it. It is called automatically every 25 millisecond.

You are getting the error because you are calling it. It is automatically called. You don't need to call it or invoke it.

Sign up to request clarification or add additional context in comments.

Comments

0

setInterval does not return a function. Modify it like this.

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

Then you can run inter() and have it run setInterval

3 Comments

Hi I've been using this code but for some reason, the y variable doesn't increment. I've set the variable to y=55 before your code and it just stays at 55
The console doesn't print any errors either. I put an alert("boom") before the if statement and it doesn't execute. It just carries on to the end and goes back to the calling function
Pass the y variable as a function parameter and call inter(y)? Might be a scoping issue.
0

Try this:

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();

1 Comment

That will throw an error as you cannot run clearInterval on inter which is the container function now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.