1

I understood that the best practice with setTimeOut is sending an anonymous function with the function I want to redo inside. Why won't it work?

function movement(dir) {
    ...
    ...
        setTimeOut(function (){movement(dir);},21);
1

1 Answer 1

3

It will work. Of course, the name of the function is setTimeout() instead of setTimeOut(). See this example:

function movement(dir) {
  console.log(dir);
  if (dir++ < 5) {
    setTimeout(function () {
      movement(dir);
    }, 21);
  }
};

movement(1);

It prints:

1
2
3
4
5
Sign up to request clarification or add additional context in comments.

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.