Function goose::util::sleep_minus_drift [−][src]
Expand description
Sleep for a specified duration, minus the time spent doing other things.
Example
use goose::util;
async fn loop_with_delay() {
loop {
// Start drift timer.
let mut drift_timer = tokio::time::Instant::now();
// Do other stuff, in this case sleep 250 milliseconds. This is
// the "drift" that will be subtracted from the sleep time later.
tokio::time::sleep(std::time::Duration::from_millis(250));
// Sleep for 1 second minus the time spent doing other stuff.
drift_timer = util::sleep_minus_drift(
std::time::Duration::from_secs(1),
drift_timer,
).await;
// Normally the loop would continue, and the amount of time doing
// other things would vary each time, but the total time to complete
// the loop would remain the same.
break;
}
}