Angular JS $timeout vs $interval

前端 未结 2 397
野性不改
野性不改 2020-12-10 00:51

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.

2条回答
  •  萌比男神i
    2020-12-10 01:39

    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.

提交回复
热议问题