Skip to main content

Overview

The useCountdown hook provides a countdown timer that updates every second and triggers a callback when the timer expires. It’s primarily used for quote expiration tracking.

Import

Signature

Parameters

number
required
Unix timestamp (in seconds) when the countdown should expire
() => void
Optional callback function to execute when the countdown reaches zero

Return Value

number
Number of seconds remaining until expiration
string
Formatted time string showing remaining seconds, or “Expired” when done
boolean
True when the countdown has reached zero

Usage Examples

Basic Countdown

Quote Expiration Timer

With Visual Progress

QuoteCountdown Component

The actual QuoteCountdown component in the application uses this hook:
components/QuoteCountdown.tsx

Implementation Details

Automatic Cleanup

The hook automatically cleans up the interval timer when:
  • The component unmounts
  • The timestamp parameter changes
  • The countdown reaches zero

Time Calculation

The countdown calculates remaining time by:
  1. Converting Unix timestamp (seconds) to milliseconds
  2. Subtracting current time
  3. Flooring to nearest second
  4. Clamping to zero minimum

Best Practices

Use with quote expiration

Quotes from the OneBalance API have a 30-second validity period. Use this hook to track expiration and automatically refresh quotes.

Provide expiration callback

Always pass an onExpire callback to handle what should happen when the timer runs out (e.g., refresh quote, show warning).

Handle edge cases

Check isExpired before allowing users to execute actions based on the countdown (e.g., prevent swap execution with expired quote).
  • useQuotes - Uses countdown for quote expiration tracking
  • QuoteDetails - Displays quote information with countdown
  • SwapForm - Uses countdown for quote validity