-2

I confused what the differences between these 2 options and if there is a better one the parameter in the timeout function is outside

function createPost(){
  setTimeout(function(post){
    posts.push(post);
  }, 2000);
}

at another function parameter inside createPost function

function createPost(post){
  setTimeout(function(){
    posts.push(post);
  }, 2000);
}

Solution:

there are 2 different things and only the 2nd option will work

itay k
  • 81
  • 1
  • 8

1 Answers1

1

setTimout's first parameter is a callback function that will be executed after the timeout. It simply calls the function. It doesn't pass any parameters to it.

The second option is the only one that will work. You'll call createPost(post) and it will push to the array after the timeout that post.

Vlad Gincher
  • 1,052
  • 11
  • 20
  • thanks and sorry about the not good question I am just a beginner – itay k Feb 07 '20 at 11:07
  • 1
    Thats ok, I'm not one of those who downvoted the question. But you should do more research before asking here, you could have tried to execute it and see that only the second one works. – Vlad Gincher Feb 07 '20 at 11:09