Skip to main content
Login

Sign in to access your account

Cooldown Button - National Design System

A button that disables itself and counts down before it can be pressed again, for resend, retry and rate-limited actions

Overview

A cooldown button disables itself after one click and counts down before it can be pressed again. Its label shows the countdown, then returns to its own label or to one you set. It is a .nds-cooldown modifier on Button, so every variant and size applies. The component owns only the cooldown and its countdown label.

Pick another component when:

  • the action has no rate limit to respect: Button

Markup

Preview
<button type="button" class="nds-btn nds-secondary nds-cooldown" data-cooldown="15" data-cooldown-label="Resend in {s}s" data-resend-label="Resend"> <span class="nds-label">Send code</span> </button>

Built-in Features

  • Auto-initialization

    Every .nds-cooldown on the page is wired at load, and any added later. No init call is needed.

  • Live Countdown Label

    The label shows the data-cooldown-label text and updates every second, with each {s} replaced by the seconds left.

  • Stable Width

    The label reserves its widest text from page load: the first label, the countdown at its longest, or the resend label. The button never resizes during the cooldown.

  • Your Loading State

    The component has no loading phase: a timer cannot know how long a response takes. Add data-state="loading" from nds:cooldown:start, and remove it when the response arrives. The state hides the label, so the countdown runs under the spinner.

  • Your Confirmation

    The component shows no alert of its own. Show one with Alert when the response arrives, in the same nds:cooldown:start listener. A failed send then never reports success.

  • Programmatic Control

    Start a cooldown from JavaScript, resume one after a page reload with the seconds left, or end one early.

Best Practices

  • Use it for a resend action with a per-user rate limit, such as a one-time code (OTP), a verification email or a password reset. Set data-cooldown to the server’s limit exactly.
  • Use it for a retry button after a failed request, so the user cannot send repeated requests to a server that is already failing.
  • Do not use it to block a double form submit. Disable the submit button while the form sends instead.
  • Do not use it for cooldowns over a few minutes. A long countdown reads as nagging and ties the user to the page. Show the time when the action is available again instead.
  • Put the button text in a <span class="nds-label">. The countdown writes into that span. A button without one never counts down and logs a console warning.
  • On an .nds-icon-only button, write an empty <span class="nds-label"></span> before the icon. The countdown takes the icon’s place in the same square, so the button keeps its size. Give the button an aria-label: the empty label gives it no name at rest.
  • Omit data-resend-label when the action reads the same every time, such as “Try again”. The button then returns to its first label.
  • Keep the countdown text short, such as “Resend in 30s”. Stable Width holds the button at its longest text, so a long countdown makes it wide at rest too. Shorten the wording, not the CSS.
  • Call NDS.Cooldown.reset() when the request fails. The user can then retry at once, instead of waiting out a cooldown for a send that failed.

API

Data Attributes

Attribute Element Effect
data-cooldown .nds-cooldown Seconds to hold the cooldown. Required, unless start() passes seconds. A value of 0 or less turns the cooldown off. Read once, when the button is wired. Editing it later has no effect
data-cooldown-label .nds-cooldown Countdown text. Every {s} is replaced by the seconds left. Default {s}: the number alone. Only {s} counts down: %s, %d and {seconds} show as typed, and a label with no {s} logs a console warning when the button is wired. Read at the start of each cooldown
data-resend-label .nds-cooldown Label shown when any cooldown ends, by the timer or by reset(). Omit it to restore the label the cooldown started with. Read at the start of each cooldown
data-state="cooldown" .nds-cooldown Set by the component, together with disabled, during the cooldown. Read it to style or query a button during its cooldown. Do not set it yourself

JavaScript

Method Effect
NDS.Cooldown.init() Wires every .nds-cooldown on the page, now and later. Runs once at load. Call it only if you disabled the loader
NDS.Cooldown.start(btn, opts?) Starts a cooldown by hand. Does nothing while a cooldown is already running
NDS.Cooldown.reset(btn) Ends the cooldown early and restores the button, as a finished cooldown does
Option Default Effect
seconds data-cooldown Seconds to run instead. With it, a button needs no data-cooldown and can run entirely from JavaScript
silent false Skip nds:cooldown:start, for a send that already happened elsewhere. tick and end still fire

All events bubble.

Event Fired on Detail
nds:cooldown:start .nds-cooldown, when the cooldown starts. Skipped by silent None. Issue the request here, and confirm from its response
nds:cooldown:tick .nds-cooldown, every second during the cooldown, starting at the full duration { remaining }: the seconds left
nds:cooldown:end .nds-cooldown, when the cooldown finishes or reset() is called None. The button is enabled again and its label restored
const btn = document.querySelector('#resend-btn'); // Add the listener before any start() call, so the first cooldown reaches it. btn.addEventListener('nds:cooldown:start', async () => { NDS.state.add(btn, 'loading'); try { // NDS.request throws on a non-OK status, so a failed send reaches // the catch below. A plain fetch resolves on one, reporting a // failed resend as though it had worked. await NDS.request('/api/resend', { method: 'POST' }); NDS.Alert.create({ status: 'success', title: 'Code sent', display: 'toast', position: 'top', duration: 4000 }); } catch (err) { NDS.Alert.create({ status: 'error', title: 'Could not send the code', display: 'toast', position: 'top', duration: 0 }); // Let the user retry at once, instead of waiting out a cooldown // for a send that failed. NDS.Cooldown.reset(btn); } finally { NDS.state.remove(btn, 'loading'); } }); // Start a cooldown by hand, such as after a form submit elsewhere. // Use one of these forms. A call during a running cooldown does nothing. NDS.Cooldown.start(btn); // Or: the send already happened. Start the cooldown without firing // nds:cooldown:start again. NDS.Cooldown.start(btn, { silent: true }); // Or: resume a cooldown after a page reload, with 20 of 30 seconds left. NDS.Cooldown.start(btn, { seconds: 20, silent: true });

The full API is in the banner of _js/nds-cooldown.js.

Last Modified Date: 05/11/2026 - 03:30 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks