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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?