JS timing events within infinite loop / interval
I have looked around, tried several solutions and have not been able to find a solution :/
What I am looking to do is to have a loop that runs every second which contains events that are fired with different delays depending on the event type and conditions such event type pertains.
Tried self building the infinite loop and logic with while(true) and counters, as well as using setInterval and setTimeout, but all result into the same end result (result: see bottom of the question (based on code examples that use setInterval and setTimeout)).
This result is due to (or at least so I believe, would be nice if someone can confirm) a situation where my callstack contains new every second invocations of the whole loop and X amount of non processed eventY's that have been invoked -> the very first run the behavior is as expected, but after this the callstack begins to accumulate the non processed event Y's and prints them out every second or per the speed of the main loop.
some example code to explain (using setInterval and setTimeout):
function start() {
setInterval(eventList, 1000);
};
function eventList() {
console.log("foo")
eventY();
};
async function eventY() {
await newTimeout(0,5).then(() => {
console.log("bar");
})
};
function newTimeout (minutes, seconds) {
let counter = (minutes*60+seconds)*1000;
return new Promise(resolve => setTimeout(resolve, counter));
}
OR more cumbersome and forceful delay (leads to unpredictable behavior at best and with longer events, the same end result):
function newTimeout(minutes, seconds) {
return new Promise(function(resolve, reject){
let counter = minutes*60+seconds;
let interval = setInterval((() => {
counter--;
if(counter == 0) {
clearInterval(interval);
resolve();
}
}), 1000)
});
};
ALSO tried using just setTimeout to stay away from the setInterval as I understand it is liable to clutter your callstack, ofc the below doesn't change anything now looking at it, but thought I would put it in anyway as I tend to prefer the thought of setTimeout that the function calls itself creating the loop that way.
function start() {
try {
eventList();
} catch (err) {
throw err
} finally {
setTimeout(() => start(), 1000);
}
};
end result is basically:
foo
foo
foo
foo
foo
bar
foo
bar
foo
bar
foo
javascript node.js settimeout setinterval
|
show 2 more comments
I have looked around, tried several solutions and have not been able to find a solution :/
What I am looking to do is to have a loop that runs every second which contains events that are fired with different delays depending on the event type and conditions such event type pertains.
Tried self building the infinite loop and logic with while(true) and counters, as well as using setInterval and setTimeout, but all result into the same end result (result: see bottom of the question (based on code examples that use setInterval and setTimeout)).
This result is due to (or at least so I believe, would be nice if someone can confirm) a situation where my callstack contains new every second invocations of the whole loop and X amount of non processed eventY's that have been invoked -> the very first run the behavior is as expected, but after this the callstack begins to accumulate the non processed event Y's and prints them out every second or per the speed of the main loop.
some example code to explain (using setInterval and setTimeout):
function start() {
setInterval(eventList, 1000);
};
function eventList() {
console.log("foo")
eventY();
};
async function eventY() {
await newTimeout(0,5).then(() => {
console.log("bar");
})
};
function newTimeout (minutes, seconds) {
let counter = (minutes*60+seconds)*1000;
return new Promise(resolve => setTimeout(resolve, counter));
}
OR more cumbersome and forceful delay (leads to unpredictable behavior at best and with longer events, the same end result):
function newTimeout(minutes, seconds) {
return new Promise(function(resolve, reject){
let counter = minutes*60+seconds;
let interval = setInterval((() => {
counter--;
if(counter == 0) {
clearInterval(interval);
resolve();
}
}), 1000)
});
};
ALSO tried using just setTimeout to stay away from the setInterval as I understand it is liable to clutter your callstack, ofc the below doesn't change anything now looking at it, but thought I would put it in anyway as I tend to prefer the thought of setTimeout that the function calls itself creating the loop that way.
function start() {
try {
eventList();
} catch (err) {
throw err
} finally {
setTimeout(() => start(), 1000);
}
};
end result is basically:
foo
foo
foo
foo
foo
bar
foo
bar
foo
bar
foo
javascript node.js settimeout setinterval
confused by your use of "seconds" whensetTimeout()
takes delay parameter in milliseconds. In thenewTImeOut()
method the parameters are calledminutes
andseconds
- which you calculate and send tosetTimeout()
- is your intent to time that out after 5ms or 5seconds?
– Randy Casburn
Nov 24 '18 at 0:40
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27
|
show 2 more comments
I have looked around, tried several solutions and have not been able to find a solution :/
What I am looking to do is to have a loop that runs every second which contains events that are fired with different delays depending on the event type and conditions such event type pertains.
Tried self building the infinite loop and logic with while(true) and counters, as well as using setInterval and setTimeout, but all result into the same end result (result: see bottom of the question (based on code examples that use setInterval and setTimeout)).
This result is due to (or at least so I believe, would be nice if someone can confirm) a situation where my callstack contains new every second invocations of the whole loop and X amount of non processed eventY's that have been invoked -> the very first run the behavior is as expected, but after this the callstack begins to accumulate the non processed event Y's and prints them out every second or per the speed of the main loop.
some example code to explain (using setInterval and setTimeout):
function start() {
setInterval(eventList, 1000);
};
function eventList() {
console.log("foo")
eventY();
};
async function eventY() {
await newTimeout(0,5).then(() => {
console.log("bar");
})
};
function newTimeout (minutes, seconds) {
let counter = (minutes*60+seconds)*1000;
return new Promise(resolve => setTimeout(resolve, counter));
}
OR more cumbersome and forceful delay (leads to unpredictable behavior at best and with longer events, the same end result):
function newTimeout(minutes, seconds) {
return new Promise(function(resolve, reject){
let counter = minutes*60+seconds;
let interval = setInterval((() => {
counter--;
if(counter == 0) {
clearInterval(interval);
resolve();
}
}), 1000)
});
};
ALSO tried using just setTimeout to stay away from the setInterval as I understand it is liable to clutter your callstack, ofc the below doesn't change anything now looking at it, but thought I would put it in anyway as I tend to prefer the thought of setTimeout that the function calls itself creating the loop that way.
function start() {
try {
eventList();
} catch (err) {
throw err
} finally {
setTimeout(() => start(), 1000);
}
};
end result is basically:
foo
foo
foo
foo
foo
bar
foo
bar
foo
bar
foo
javascript node.js settimeout setinterval
I have looked around, tried several solutions and have not been able to find a solution :/
What I am looking to do is to have a loop that runs every second which contains events that are fired with different delays depending on the event type and conditions such event type pertains.
Tried self building the infinite loop and logic with while(true) and counters, as well as using setInterval and setTimeout, but all result into the same end result (result: see bottom of the question (based on code examples that use setInterval and setTimeout)).
This result is due to (or at least so I believe, would be nice if someone can confirm) a situation where my callstack contains new every second invocations of the whole loop and X amount of non processed eventY's that have been invoked -> the very first run the behavior is as expected, but after this the callstack begins to accumulate the non processed event Y's and prints them out every second or per the speed of the main loop.
some example code to explain (using setInterval and setTimeout):
function start() {
setInterval(eventList, 1000);
};
function eventList() {
console.log("foo")
eventY();
};
async function eventY() {
await newTimeout(0,5).then(() => {
console.log("bar");
})
};
function newTimeout (minutes, seconds) {
let counter = (minutes*60+seconds)*1000;
return new Promise(resolve => setTimeout(resolve, counter));
}
OR more cumbersome and forceful delay (leads to unpredictable behavior at best and with longer events, the same end result):
function newTimeout(minutes, seconds) {
return new Promise(function(resolve, reject){
let counter = minutes*60+seconds;
let interval = setInterval((() => {
counter--;
if(counter == 0) {
clearInterval(interval);
resolve();
}
}), 1000)
});
};
ALSO tried using just setTimeout to stay away from the setInterval as I understand it is liable to clutter your callstack, ofc the below doesn't change anything now looking at it, but thought I would put it in anyway as I tend to prefer the thought of setTimeout that the function calls itself creating the loop that way.
function start() {
try {
eventList();
} catch (err) {
throw err
} finally {
setTimeout(() => start(), 1000);
}
};
end result is basically:
foo
foo
foo
foo
foo
bar
foo
bar
foo
bar
foo
javascript node.js settimeout setinterval
javascript node.js settimeout setinterval
edited Nov 24 '18 at 9:24
Jontsu
asked Nov 24 '18 at 0:26
JontsuJontsu
285
285
confused by your use of "seconds" whensetTimeout()
takes delay parameter in milliseconds. In thenewTImeOut()
method the parameters are calledminutes
andseconds
- which you calculate and send tosetTimeout()
- is your intent to time that out after 5ms or 5seconds?
– Randy Casburn
Nov 24 '18 at 0:40
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27
|
show 2 more comments
confused by your use of "seconds" whensetTimeout()
takes delay parameter in milliseconds. In thenewTImeOut()
method the parameters are calledminutes
andseconds
- which you calculate and send tosetTimeout()
- is your intent to time that out after 5ms or 5seconds?
– Randy Casburn
Nov 24 '18 at 0:40
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27
confused by your use of "seconds" when
setTimeout()
takes delay parameter in milliseconds. In the newTImeOut()
method the parameters are called minutes
and seconds
- which you calculate and send to setTimeout()
- is your intent to time that out after 5ms or 5seconds?– Randy Casburn
Nov 24 '18 at 0:40
confused by your use of "seconds" when
setTimeout()
takes delay parameter in milliseconds. In the newTImeOut()
method the parameters are called minutes
and seconds
- which you calculate and send to setTimeout()
- is your intent to time that out after 5ms or 5seconds?– Randy Casburn
Nov 24 '18 at 0:40
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27
|
show 2 more comments
0
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53454182%2fjs-timing-events-within-infinite-loop-interval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
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.
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%2f53454182%2fjs-timing-events-within-infinite-loop-interval%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
confused by your use of "seconds" when
setTimeout()
takes delay parameter in milliseconds. In thenewTImeOut()
method the parameters are calledminutes
andseconds
- which you calculate and send tosetTimeout()
- is your intent to time that out after 5ms or 5seconds?– Randy Casburn
Nov 24 '18 at 0:40
Promises are only ever resolved once, it's not something you would use in a setInterval.
– Keith
Nov 24 '18 at 0:41
When you say, "a loop that runs every second which contains events that are fired with different delays" so you mean the loop really runs every second or runs with a gap of a second after the events have all fired. In other words can a new loop start before the inner events have finished?
– Mark Meyer
Nov 24 '18 at 0:44
Randy: oops forgot to put *1000 on the example, fixed now to: let counter = (minutes*60+seconds)*1000; -> won't make a difference re. the problem though.
– Jontsu
Nov 24 '18 at 9:26
Keith can you expand? This sounds like it is in the right tracks of answering the why this happens.
– Jontsu
Nov 24 '18 at 9:27