pub fn schedule_interval<'h, F>(
    interval: Duration,
    hint: Option<CallbackHint>,
    handler: F
) -> Result<TimerHandle<'h>>where
    F: FnMut() + Send + 'h,
Expand description

Schedule an interval task.

The function interval cannot be negative (it will be converted to u32). CallbackHint gives the OS scheduler some hint about the callback behavior. For example, if the handler takes long time to finish, the caller should hint with CallbackHint::SlowFunction. If no hint is specified, it’s up to the scheduler to decide.

Following example doesn’t give a hint.

let mut count = 0;
let t = schedule_interval(Duration::from_millis(200), None, || count += 1);
sleep(Duration::from_millis(500));
drop(t);
assert_eq!(count, 2);
Examples found in repository?
src/examples/simple.rs (line 6)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let t1 = schedule_interval(Duration::from_secs(1), None, || println!("Tick!")).unwrap();
    let t2 = schedule_interval(Duration::from_secs(1), Some(CallbackHint::QuickFunction), || println!("Tick fast!")).unwrap();
    let t3 = schedule_oneshot(Duration::from_millis(800), Some(CallbackHint::QuickFunction), || println!("One shot!")).unwrap();

    let mut lazy_count = 0;
    let t4 = schedule_interval(Duration::from_secs(1), Some(CallbackHint::SlowFunction), || {
        lazy_count += 1;
        let my_id = lazy_count;
        println!("I'm lazy... #{my_id}");
        sleep(Duration::from_secs(3));
        println!("#{my_id} I may have been called with a dedicated thread");
    }).unwrap();

    println!("Wait for timer working");
    sleep(Duration::from_secs(5));

    println!("Dropping timers");

    // Note that every dropping will block the thread because dropping timer needs to ensure the execution of timer function.
    drop(t1);
    drop(t2);
    drop(t3);
    drop(t4);

    println!("That's it!");
}