1

I know this may be an old question. But I'm really confused after a few googling.

From this question, I learn the setTimeout will execute forever,

but from this one, I learn it will execute only once.

What's strange is that when I test it in browser console, I happen to see it keeps executing..

enter image description here

But sometimes it only execute once:

enter image description here

The same code gives different result. Anyone know why?

EDITED: I now can believe setTimeout executes only once, but how to explain the first screenshot of my test?

Community
  • 1
  • 1
牛さん
  • 2,884
  • 3
  • 29
  • 43
  • *"From this question, I learn the setTimeout will execute forever"*: If you are referring to the accepted answer, I think it actually refers to the function defined in the question, not `setTimeout`. – Felix Kling May 02 '13 at 03:03

2 Answers2

5

setTimeout will only execute once.

The code in the stackoverflow question you linked to keeps executing because it is recursive. The setTimeout call, calls itself.

Coin_op
  • 10,568
  • 4
  • 35
  • 46
  • its tricky in my case doing like this $(".btn-new-order").trigger('change'); was causing lot of issues – shareef Dec 07 '17 at 08:39
1

setTimeout does not execute forever, though as demonstrated in the linked question, you can emulate setInterval by calling setTimeout again in the function you pass to setTimeout

This answer explains that the return value from setTimeout/setInterval is one that you can use to refer to the timer later to cancel the timeout.

Community
  • 1
  • 1
kahowell
  • 27,286
  • 1
  • 14
  • 9