Calling a function every 60 seconds

后端 未结 13 2101
情书的邮戳
情书的邮戳 2020-11-22 08:24

Using setTimeout() it is possible to launch a function at a specified time:

setTimeout(function, 60000);

But what if I would l

13条回答
  •  一生所求
    2020-11-22 09:14

    // example:
    // checkEach(1000, () => {
    //   if(!canIDoWorkNow()) {
    //     return true // try again after 1 second
    //   }
    //
    //   doWork()
    // })
    export function checkEach(milliseconds, fn) {
      const timer = setInterval(
        () => {
          try {
            const retry = fn()
    
            if (retry !== true) {
              clearInterval(timer)
            }
          } catch (e) {
            clearInterval(timer)
    
            throw e
          }
        },
        milliseconds
      )
    }
    

提交回复
热议问题