I noticed a couple of issues with your code.
You are calling
setTimeoutin a loop. So if you have, say,
numOfbox.length === 15,
setTimeoutwill be called 15 times around
10,000ms from when you set it. Could this be the reason you’re seeing the more calls to
startGamethan you thought?
I see the variable
numOfbox, but since it’s not declared in the
startGamefunction I’ll have to assume that it’s in the parent scope. So, in the line where you do
timeout[i] = setTimeout(startGame, 10000, numOfbox);
Realize that since
numOfbox is in a higher level scope and
startGame does not take any parameters, the
numOfbox parameter (the 3rd argument in
setTimeout) is really not going anywhere: the
numOfbox variable actually uses is coming from the parent scope. This may be ok, but you should consider what is happening here.