Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  29] [ 4]  / answers: 1 / hits: 32488  / 7 Years ago, tue, july 25, 2017, 12:00:00

I am trying to use setInterval in my Angular 4 app.



const inter = setInterval(() => {
// logic resulting in exitCondition
if(exitCondition) {
clearInterval(inter);
}
}, 1000);


This set up works fine in vanilla javascript, but clearInterval() does not seem to work in Angular. Upon doing some research I found an interval service for Angular 1.x :



https://docs.angularjs.org/api/ng/service/$interval



Is there anything similar for Angular 4? Or is there a workaround to make clearInterval() work?


More From » angular

 Answers
7

You can set like this,



  this.interval = setInterval(() => {

}, 1000);


and clear like this,



if (this.interval) {
clearInterval(this.interval);
}

[#56987] Thursday, July 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;