new

Function new 

Source
pub fn new(timeout: Duration) -> Model
Expand description

Creates a new timer with the specified timeout and default 1-second interval.

This is the most common way to create a timer. It uses a sensible default of 1-second intervals, which provides a good balance between display smoothness and resource usage for most applications.

§Arguments

  • timeout - The initial countdown duration (how long until the timer expires)

§Returns

A new Model instance with 1-second tick intervals

§Examples

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

// Create a 30-second countdown timer
let timer = new(Duration::from_secs(30));
assert_eq!(timer.timeout, Duration::from_secs(30));
assert_eq!(timer.interval, Duration::from_secs(1)); // Default interval
assert!(timer.running());
assert!(!timer.timedout());

Common timer durations:

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

// Short timer for notifications
let notification = new(Duration::from_secs(5));

// Medium timer for breaks
let break_timer = new(Duration::from_secs(300)); // 5 minutes

// Long timer for cooking
let cooking_timer = new(Duration::from_secs(1800)); // 30 minutes

// Sub-second timer for quick actions
let quick_timer = new(Duration::from_millis(750));

Integration with Bubble Tea:

use bubbletea_widgets::timer::new;
use bubbletea_rs::{Model as BubbleTeaModel, Cmd};
use std::time::Duration;

struct App {
    timer: bubbletea_widgets::timer::Model,
}

impl BubbleTeaModel for App {
    fn init() -> (Self, Option<Cmd>) {
        let timer = new(Duration::from_secs(60));
        let init_cmd = timer.init(); // Start the timer
        (App { timer }, Some(init_cmd))
    }
     
    // ... other methods
}

§Default Configuration

  • Interval: 1 second (good balance of smoothness and efficiency)
  • State: Running (timer starts immediately)
  • ID: Unique identifier generated automatically
  • Display: Shows remaining time in Go’s duration format

§When to Use

Use this function when:

  • You want standard 1-second timer updates
  • Resource efficiency is important
  • You don’t need sub-second display precision
  • Building typical countdown or timeout functionality

Use new_with_interval() instead when you need:

  • Smoother display updates (< 1 second intervals)
  • High-precision timing
  • Custom update frequencies

§Note

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