Function Timer method or library

Is there an easy way to only execute a function or block of code every n seconds without having to do the standard if cur_time > last_time + duration logic?

Well, you could do something like this:

use std::thread;
use std::time::Duration;
for _ in 0..n {
    thread::spawn(move || {/**/});
    thread::sleep(Duration::from_millis(1000));
}

But that would lock you to not working with references to local scope objects, which might midigate you. Otherwise you will probably have to use your thread::sleep(wanted - elapsed); logic either inline or in a helper function taking <F: FnMut()>.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.