If you write like this, you might as well remove setTimeout completely as it's not doing anything here (try setting timeout to 10s and see if it prints out the result after 10s).
What you are doing is this:
This will just print out all index immediately and setTimeout will have no effect.
To fix this - you either need to return a function, or using bind when setting a timeout, e.g.:
The code in the final example is actually wrong:
If you write like this, you might as well remove
setTimeoutcompletely as it's not doing anything here (try setting timeout to 10s and see if it prints out the result after 10s).What you are doing is this:
This will just print out all index immediately and
setTimeoutwill have no effect.To fix this - you either need to return a function, or using
bindwhen setting a timeout, e.g.:Oops. you are totally correct, forgot to return a function!. I appreciate you pointing this out. Will fix the code example!