File tree Expand file tree Collapse file tree 3 files changed +68
-3
lines changed
Expand file tree Collapse file tree 3 files changed +68
-3
lines changed Original file line number Diff line number Diff line change @@ -39,15 +39,16 @@ impl<T> PartialEq for UseDebounce<T> {
3939/// Once the [`UseDebounce::action`] method is called, a timer will start counting down until
4040/// the callback is ran. If the [`UseDebounce::action`] method is called again, the timer will restart.
4141///
42- /// # Example
42+ /// # Examples
4343///
4444/// ```rust
4545/// use dioxus::prelude::*;
4646/// use dioxus_time::use_debounce;
4747/// use std::time::Duration;
4848///
49+ /// #[component]
4950/// fn App() -> Element {
50- /// let mut debounce = use_debounce(Duration::from_millis(2000 ), |_| println!("ran"));
51+ /// let mut debounce = use_debounce(Duration::from_secs(2 ), |_| println!("ran"));
5152///
5253/// rsx! {
5354/// button {
Original file line number Diff line number Diff line change 11use dioxus:: prelude:: { use_hook, Callback , Writable } ;
22use std:: time:: Duration ;
33
4+ /// A handle to an interval.
5+ ///
6+ /// This handle allows you to cancel an interval.
47#[ derive( Clone , PartialEq , Copy ) ]
58pub struct UseInterval {
69 inner : dioxus:: prelude:: Signal < InnerUseInterval > ,
@@ -28,6 +31,25 @@ impl UseInterval {
2831}
2932
3033/// Repeatedly call a function at a specific interval.
34+ ///
35+ /// Intervals are cancelable with the [`UseInterval::cancel`] method.
36+ ///
37+ /// # Examples
38+ /// ```rust
39+ /// use dioxus::prelude::*;
40+ /// use dioxus_time::use_interval;
41+ /// use std::time::Duration;
42+ ///
43+ /// #[component]
44+ /// fn App() -> Element {
45+ /// let mut time_elapsed = use_signal(|| 0);
46+ /// use_interval(Duration::from_secs(1), move || *time_elapsed.write() += 1);
47+ ///
48+ /// rsx! {
49+ /// "It has been {time_elapsed} since the app started."
50+ /// }
51+ /// }
52+ /// ```
3153pub fn use_interval ( period : Duration , mut action : impl FnMut ( ) + ' static ) -> UseInterval {
3254 let inner = use_hook ( || {
3355 let callback = Callback :: new ( move |( ) | {
Original file line number Diff line number Diff line change 1- //! Dioxus time utilities.
1+ //! # Dioxus Time Utilities
2+ //!
3+ //! Cross-platform timing utilities for your Dioxus apps.
4+ //!
5+ //! We currently offer:
6+ //! - [`use_debounce`]
7+ //! - [`use_interval`]
8+ //! - and [`sleep`]
9+ #![ warn( missing_docs) ]
10+
11+ use std:: time:: Duration ;
212
313mod interval;
414pub use interval:: { use_interval, UseInterval } ;
515
616mod debounce;
717pub use debounce:: { use_debounce, UseDebounce } ;
18+
19+
20+ /// Pause the current task for the specified duration.
21+ ///
22+ /// # Examples
23+ /// ```rust
24+ /// use std::time::Duration;
25+ /// use dioxus::prelude::*;
26+ ///
27+ /// #[component]
28+ /// pub fn App() -> Element {
29+ /// let mut has_slept = use_signal(|| false);
30+ ///
31+ /// use_effect(move || {
32+ /// spawn(async move {
33+ /// dioxus_time::sleep(Duration::from_secs(3)).await;
34+ /// has_slept.set(true);
35+ /// });
36+ /// });
37+ ///
38+ /// rsx! {
39+ /// "I have slept: {has_slept}"
40+ /// }
41+ /// }
42+ /// ```
43+ pub async fn sleep ( duration : Duration ) {
44+ #[ cfg( not( target_family = "wasm" ) ) ]
45+ tokio:: time:: sleep ( duration) . await ;
46+
47+ #[ cfg( target_family = "wasm" ) ]
48+ gloo_timers:: future:: sleep ( duration) . await ;
49+ }
You can’t perform that action at this time.
0 commit comments