• bleistift2@sopuli.xyz
    link
    fedilink
    Deutsch
    arrow-up
    2
    ·
    6 months ago

    Ich frage mich echt, wie man diesen Cooldown so verkacken kann.

    const MAX_QUOTA = 6
    const COOLDOWN_PERIOD_MS = 30000
    let quota = 0
    let interval = null
    
    function regrow() {
      if (quota >= MAX_QUOTA) {
        clearInterval(interval)
        interval = null
        return
      }
      quota++
    }
    
    function onClick() {
      if (quota <= 0) { return }
    
      fetch(/**/)
        .then((res) => {
          if (!res.ok) { return Promise.reject(res) }
          quota--
          if (!interval) {
            regrow()
          }
        })
    }
    
    interval = setInterval(regrow, COOLDOWN_PERIOD_MS)