26

What's the difference? When should I use which? I am new to Angular & they look very similar to me. I would rather follow best practices from the start.

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

2 Answers2

63

$interval executes a callback repeatedly, while $timeout simply delays the execution of a callback (doesn't repeat). So, no, they're not the same. Additionally, it should be noted that both of them are wrappers for window.setInterval and window.setTimeout respectively.

I would also like to recommend to read this great article written by John Resig on how Javascript timers work.

Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
3

Here's some info extracted from djvirgen's response to a similar Reddit question:

You should always be using $timeout in Angular apps. Here's why:

  • It's injectable, making testing easier with ngMock.
  • It runs a digest to ensure your view is updated.
  • It is thenable (it's also a promise).

However, if you don't want a digest to run, you can simply pass false as the third argument.

I would guess $interval has similar advantages.

Community
  • 1
  • 1
Batandwa
  • 530
  • 4
  • 7
  • 18