dioxus_time/lib.rs
1//! # Dioxus Time Utilities
2//!
3//! Cross-platform timing utilities for your Dioxus apps.
4//!
5//! We currently offer:
6//! - [`use_timeout`]
7//! - [`use_debounce`]
8//! - [`use_interval`]
9//! - and [`sleep`]
10#![warn(missing_docs)]
11
12use std::time::Duration;
13
14mod interval;
15pub use interval::{UseInterval, use_interval};
16
17mod debounce;
18pub use debounce::{UseDebounce, use_debounce};
19
20mod timeout;
21pub use timeout::{TimeoutHandle, UseTimeout, use_timeout};
22
23/// Pause the current task for the specified duration.
24///
25/// # Examples
26/// ```rust
27/// use std::time::Duration;
28/// use dioxus::prelude::*;
29///
30/// #[component]
31/// pub fn App() -> Element {
32/// let mut has_slept = use_signal(|| false);
33///
34/// use_effect(move || {
35/// spawn(async move {
36/// dioxus_time::sleep(Duration::from_secs(3)).await;
37/// has_slept.set(true);
38/// });
39/// });
40///
41/// rsx! {
42/// "I have slept: {has_slept}"
43/// }
44/// }
45/// ```
46pub async fn sleep(duration: Duration) {
47 #[cfg(not(target_family = "wasm"))]
48 tokio::time::sleep(duration).await;
49
50 #[cfg(target_family = "wasm")]
51 gloo_timers::future::sleep(duration).await;
52}