• 25 Posts
  • 51 Comments
Joined 1 month ago
cake
Cake day: June 2nd, 2024

help-circle
  • bleistift2@sopuli.xyztoich_iel@feddit.orgich🖍️iel
    link
    fedilink
    Deutsch
    arrow-up
    2
    ·
    17 hours 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)