It supports the expected setInterval parameters with an extra optional one for specifying the number of repetitions to be done. So I implemented my own setInterval function which tackles the above mentioned shortcomings. On top of the previously mentioned shortcomings, I'd really appreciate if setInterval had an optional parameter for setting the number of repetitions instead of repeating infinitely. If for some reason, an error occurs in part of the code that is called by setInterval, it wouldn't break and keeps executing the faulty code. To mitigate the potential impact this can have on performance, once intervals are nested beyond five levels deep, the browser will automatically enforce a 4 ms minimum value for the interval. What would happen is that you'll end up with a bunch of queued requests that may not necessarily return in order. It's possible for intervals to be nested that is, the callback for setInterval() can in turn call setInterval() to start another interval running, even though the first one is still going. Consider using setInterval to poll a remote server every 5 seconds, network latency, unresponsive server and other variables might prevent the request from completing on time. In some cases, the function might need longer than the interval time to finish execution. Why is setInterval evil? Doesn't care whether the callback is still running or not Despite being useful in many applications, setInterval's shortcomings could blow up your application in some circumstances. This is were the native setInterval function comes to use. SetIntervalAsync is a drop-in replacement of setInterval which shares the same API but is safe to use with non-reentrant, asynchronous functions.If you use Javascript often you might have come across the need for delaying the execution of code within intervals of time with repetition. functions that are not designed to allow multiple executions at the same time. This is often a problem for non-reentrant functions ie. This may cause problems whenever the function takes longer to execute than the given interval, since it will be called again before the first execution has finished. SetInterval runs a given function repeateadly, once every fixed number of milliseconds. If you've ever had to deal with weird, subtle bugs as a consequence of using setInterval on asynchronous functions, or had to manually reimplement setInterval using setTimeout to prevent multiple executions of the same asynchronous function from overlapping, then this library is a drop-in replacement that will solve your issues. You can require a specific strategy for setIntervalAsync using CommonJS with the following snippets: When in doubt, the Dynamic strategy will likely suffice for most use cases, keeping the interval as close as possible to the desired one. You can choose whichever strategy works best for your application. If any execution takes longer than the desired interval, the next execution is delayed until the previous one has finished, and called immediately after this condition is reached.įixed: The given function is called repeatedly, guaranteeing a fixed delay of interval milliseconds between the end of one execution and the start of the following one. SetIntervalAsync provides two strategies which can be used to prevent a recurring function from executing more than once at any given moment:ĭynamic: If possible, the given function is called once every interval milliseconds. See Dynamic and Fixed setIntervalAsync for more details. You can choose whether you wish to use the Dynamic or Fixed strategies, which will either launch every execution as soon as possible or set a fixed delay between the end of one execution and the start of the next one. Since setIntervalAsync will guarantee that the function is never executed more than once at any given moment. SetIntervalAsync ( processQueue, 1000, queue )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |