DEV Community

HelpMichaelCode
HelpMichaelCode

Posted on

1

setTimeouts inside a for loop

So I've encountered a problem that I've been trying to solve for a week now... Say I have a setTimeout inside a for loop. Inside the setTimeout function I have -> setTimeout(callback, timer, parameters of the callback). My problem is that, the values of the parameters aren't always the values that I am expecting and it seems to be that their values are always getting overwritten on each execution. My question is, how can I avoid this in a way where the values of the parameters aren't overwritten. I know this has something to do with scope but I'm not too sure how to approach it or do it.

Top comments (1)

Collapse
 
kosich profile image
Kostia Palchyk β€’

Hi, Michael! This is a classic closures question for JS!

See this stackoverflow thread for details:
stackoverflow.com/questions/522628...

Ping me if you still have questions πŸ™‚

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay