setInterval within for loop having separate instances in javascript Angular
up vote
0
down vote
favorite
I am building an ionic app and within the constructor of component I have a loop that iterate through all the items. Within the loop I have written setInterval that calls a function from within as shown below.
var thisScope = this;
for (let i = 0; i < this.schedules.length; i++) {
(function() {
thisScope.showTime[i] = new Date();
thisScope.showTime[i].setHours(0, 0, 0);
thisScope.schedules[i].afh.timerIncDec = '00:00:00';
if (!thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] !== 0 || thisScope.schedules[i].afh.timerIncDec != '00:00:00') {
thisScope.timerId = setInterval(() => {
thisScope.timerTick(thisScope.schedules[i]);
}, 1000);
} else if (thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] === 0 || thisScope.schedules[i].afh.timerIncDec === '00:00:00') {
clearInterval(thisScope.timerId);
}
})();
}
Here I want to set the timer (its calculations is being done within timerTick
function). Here the problem I am facing is setInterval that is being overlapped and its speed gets increased less than 1000
. Each item of loop should maintain its own instance of setInterval
.
The variable that is being used as increment and decrement is timerInSeconds
that is static written within same component.
Here the 1000
value is being overlapped by other setIntervals within same loop or same loop having more schedules.
javascript typescript
add a comment |
up vote
0
down vote
favorite
I am building an ionic app and within the constructor of component I have a loop that iterate through all the items. Within the loop I have written setInterval that calls a function from within as shown below.
var thisScope = this;
for (let i = 0; i < this.schedules.length; i++) {
(function() {
thisScope.showTime[i] = new Date();
thisScope.showTime[i].setHours(0, 0, 0);
thisScope.schedules[i].afh.timerIncDec = '00:00:00';
if (!thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] !== 0 || thisScope.schedules[i].afh.timerIncDec != '00:00:00') {
thisScope.timerId = setInterval(() => {
thisScope.timerTick(thisScope.schedules[i]);
}, 1000);
} else if (thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] === 0 || thisScope.schedules[i].afh.timerIncDec === '00:00:00') {
clearInterval(thisScope.timerId);
}
})();
}
Here I want to set the timer (its calculations is being done within timerTick
function). Here the problem I am facing is setInterval that is being overlapped and its speed gets increased less than 1000
. Each item of loop should maintain its own instance of setInterval
.
The variable that is being used as increment and decrement is timerInSeconds
that is static written within same component.
Here the 1000
value is being overlapped by other setIntervals within same loop or same loop having more schedules.
javascript typescript
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am building an ionic app and within the constructor of component I have a loop that iterate through all the items. Within the loop I have written setInterval that calls a function from within as shown below.
var thisScope = this;
for (let i = 0; i < this.schedules.length; i++) {
(function() {
thisScope.showTime[i] = new Date();
thisScope.showTime[i].setHours(0, 0, 0);
thisScope.schedules[i].afh.timerIncDec = '00:00:00';
if (!thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] !== 0 || thisScope.schedules[i].afh.timerIncDec != '00:00:00') {
thisScope.timerId = setInterval(() => {
thisScope.timerTick(thisScope.schedules[i]);
}, 1000);
} else if (thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] === 0 || thisScope.schedules[i].afh.timerIncDec === '00:00:00') {
clearInterval(thisScope.timerId);
}
})();
}
Here I want to set the timer (its calculations is being done within timerTick
function). Here the problem I am facing is setInterval that is being overlapped and its speed gets increased less than 1000
. Each item of loop should maintain its own instance of setInterval
.
The variable that is being used as increment and decrement is timerInSeconds
that is static written within same component.
Here the 1000
value is being overlapped by other setIntervals within same loop or same loop having more schedules.
javascript typescript
I am building an ionic app and within the constructor of component I have a loop that iterate through all the items. Within the loop I have written setInterval that calls a function from within as shown below.
var thisScope = this;
for (let i = 0; i < this.schedules.length; i++) {
(function() {
thisScope.showTime[i] = new Date();
thisScope.showTime[i].setHours(0, 0, 0);
thisScope.schedules[i].afh.timerIncDec = '00:00:00';
if (!thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] !== 0 || thisScope.schedules[i].afh.timerIncDec != '00:00:00') {
thisScope.timerId = setInterval(() => {
thisScope.timerTick(thisScope.schedules[i]);
}, 1000);
} else if (thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] === 0 || thisScope.schedules[i].afh.timerIncDec === '00:00:00') {
clearInterval(thisScope.timerId);
}
})();
}
Here I want to set the timer (its calculations is being done within timerTick
function). Here the problem I am facing is setInterval that is being overlapped and its speed gets increased less than 1000
. Each item of loop should maintain its own instance of setInterval
.
The variable that is being used as increment and decrement is timerInSeconds
that is static written within same component.
Here the 1000
value is being overlapped by other setIntervals within same loop or same loop having more schedules.
javascript typescript
javascript typescript
edited Nov 14 at 5:20
asked Nov 13 at 14:03
Umair Jameel
467826
467826
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43
add a comment |
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282750%2fsetinterval-within-for-loop-having-separate-instances-in-javascript-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi, this is just js and has very little to do with Angular or even typescript. I think you should add proper tags
– Sergey Rudenko
Nov 13 at 14:41
Can you share more of context what you are trying to achieve? Is it like race of timers?
– Sergey Rudenko
Nov 13 at 14:43