Home > Enterprise >  How to change a settimeout into a function instead of a string
How to change a settimeout into a function instead of a string

Time:09-25

Here are lines 38-41 of my code for a snake game on JS> I am using JsFiddle

createSnake();
locateApple();
setTimeout("gameCycle()", DELAY)

The setTimeout has the error: Implied eval consider passing a function instead of a string.

How should I go about fixing this?

CodePudding user response:

You should use like this: setTimeout(gameCycle, DELAY)

CodePudding user response:

Please Refer https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

The setTimeout() accepts function or code as first parameter. Using code is not recommended. Use function like below,

function gameCycle() {}

setTimeout(gameCycle, 3000); // if your delay is 3s

// OR

setTimeout(function() {}, 3000);
  • Related