rename to promiseInterval
This commit is contained in:
parent
5b403ba7d1
commit
3fb35e8123
7 changed files with 15 additions and 15 deletions
|
@ -1,28 +0,0 @@
|
|||
|
||||
// makeFetcher - replacement for setInterval for fetching, starts counting
|
||||
// the interval only after a request is done instead of immediately.
|
||||
// - promiseCall is a function that returns a promise, it's called the first
|
||||
// time after the first interval.
|
||||
// - interval is the interval delay in ms.
|
||||
|
||||
export const makeFetcher = (promiseCall, interval) => {
|
||||
let stopped = false
|
||||
let timeout = null
|
||||
let func = () => {}
|
||||
|
||||
func = () => {
|
||||
promiseCall().finally(() => {
|
||||
if (stopped) return
|
||||
timeout = window.setTimeout(func, interval)
|
||||
})
|
||||
}
|
||||
|
||||
const stopFetcher = () => {
|
||||
stopped = true
|
||||
window.clearTimeout(timeout)
|
||||
}
|
||||
|
||||
timeout = window.setTimeout(func, interval)
|
||||
|
||||
return stopFetcher
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue