0

I thought this would be the kind of thing there would be plenty of tutorials on, however every one I found was either over-complicated, vague, or used features not yet widely supported. So, I decided to try it on my own. My code is below.

function drawStuff(){
var x = document.getElementById("myCanvas");
var canvas = x.getContext('2d');

var i;

function animate(){
    canvas.clearRect(0,0,500,500);
    canvas.fillStyle="red";
    canvas.fillRect(0,i,50,50);
    i++;
}

    windows.setInterval(animate(), 125);
}
window.addEventListener("load", drawStuff, false);

Why isn't this working? Do I misunderstand what the setInterval does? Any help is appreciated.

1
  • A little late, but in line 2 it is preferred to put context not canvas Commented Jun 1, 2015 at 20:30

2 Answers 2

1

Please, set value to variable i

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

3 Comments

Which value? There's several
initialize your variable i to 0 or whatever starting value you need.
Ah. This(along with steveukx pointing out a typo)fixed it. Many thanks!
1

There is a typo in that you refer to windows rather than window, and the argument supplied should be a reference to the function you want to run, so it should be animate rather than animate() which would just call the animate function immediately.

Those two changes should make:

window.setInterval(animate, 125);

Comments

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.