shadow-function
Version:
ioing lib - shadow Function, worker Function
43 lines (34 loc) • 713 B
text/typescript
'use strict'
type options = {
polls: number
timeout: number
}
class TryAgain {
public polls: number
private task: Function
private timeout: number
private timeoutId: any
constructor (task: Function, options: options) {
this.task = task
this.polls = options.polls || 2
this.timeout = options.timeout || 3000
this.timeoutId = null
}
try () {
if (this.polls <= 0) return false
this.timeoutId = setTimeout(() => {
if (this.polls-- > 0) this.task() && this.try()
}, this.timeout)
}
stop () {
clearTimeout(this.timeoutId)
}
wait (time: number) {
this.timeout = time
}
over () {
this.stop()
this.polls = 0
}
}
export { TryAgain }