This isn't so much a problem with setInterval per se as with repeatedly executing (potentially) expensive operations without any supervision.
setInterval
To prevent this, one could simply build a wrapper like this:
const interval = (callback, timeout) => { setTimeout(() => { const result = callback() if (result && ("then" in result)) result.then(() => interval(callback, timeout)) else interval(callback, timeout) }, timeout) }
And try it out like this:
interval(() => new Promise(resolve => { console.log("Starting...") setTimeout(() => { console.log("...Finished") resolve() }, 1e3) }), 1e3)
Yes using nested setTimeout is one way to bypass this. You anyways gotta be careful when you do this. Thanks for sharing :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This isn't so much a problem with
setInterval
per se as with repeatedly executing (potentially) expensive operations without any supervision.To prevent this, one could simply build a wrapper like this:
And try it out like this:
Yes using nested setTimeout is one way to bypass this. You anyways gotta be careful when you do this. Thanks for sharing :)