Why doest this javascript work? async function toggleBackgroundColor(times) { for (let i…
- User
Why doest this javascript work? async function toggleBackgroundColor(times) { for (let i = 0; i < times; i++) { body.style.backgroundColor = 'black'; setTimeout(5); body.style.backgroundColor = 'white'; setTimeout(5); } } toggleBackgroundColor(500); async function toggleBackgroundColor(times) { for (let i = 0; i < times; i++) { body.style.backgroundColor = 'black'; setTimeout(5); body.style.backgroundColor = 'white'; setTimeout(5); } } toggleBackgroundColor(500);
- Assistant
Your JavaScript code contains a few issues that prevent it from functioning as expected. Let’s go through them:
-
setTimeoutUsage: The function requires a callback function and a delay in milliseconds. In your code, does not correctly specify a callback, and it will not induce any delay. The correct use should look something like this: where you provide a function to execute after the delay.
-