new_with_interval

Function new_with_interval 

Source
pub fn new_with_interval(timeout: Duration, interval: Duration) -> Model
Expand description

Creates a new timer with custom timeout and tick interval.

This function provides full control over timer behavior by allowing you to specify both the initial countdown duration and how frequently the timer updates. Use this when you need precise control over timing granularity or want smoother display updates.

§Arguments

  • timeout - The initial countdown duration (how long until the timer expires)
  • interval - How frequently the timer ticks and updates its display

§Returns

A new Model instance configured with the specified timing parameters

§Examples

use bubbletea_widgets::timer::new_with_interval;
use std::time::Duration;

// Create a 30-second timer that updates every 100ms (smooth display)
let smooth_timer = new_with_interval(
    Duration::from_secs(30),
    Duration::from_millis(100)
);
assert_eq!(smooth_timer.timeout, Duration::from_secs(30));
assert_eq!(smooth_timer.interval, Duration::from_millis(100));
assert!(smooth_timer.running());

Different use cases:

use bubbletea_widgets::timer::new_with_interval;
use std::time::Duration;

// High-precision timer for animations (60 FPS)
let animation_timer = new_with_interval(
    Duration::from_secs(5),
    Duration::from_millis(16) // ~60 FPS
);

// Battery-friendly timer for long countdowns
let efficient_timer = new_with_interval(
    Duration::from_secs(3600), // 1 hour
    Duration::from_secs(5)     // Update every 5 seconds
);

// Precise scientific timer
let precise_timer = new_with_interval(
    Duration::from_millis(500),
    Duration::from_millis(10)
);

§Performance Considerations

  • Smaller intervals provide smoother display but use more CPU and battery
  • Larger intervals are more efficient but may appear jerky
  • Consider your application’s needs when choosing interval duration
  • For display-only timers, 100ms-1000ms intervals work well
  • For animations, 16ms (60 FPS) provides smooth motion

§Timing Accuracy

The timer’s accuracy depends on the underlying system’s timer resolution and the Bubble Tea framework’s message processing speed. Very small intervals (< 10ms) may not be achievable on all systems.

§Note

This function matches Go’s NewWithInterval function exactly for compatibility with existing bubbles applications.