-
I try to use background loop like setInterval and clearInterval. Please advise and help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The recommended approach is to use |
Beta Was this translation helpful? Give feedback.
-
Try use_interval from yew_hooks: https://github.com/jetli/yew-hooks use yew::prelude::*;
use yew_hooks::use_interval;
/// `use_interval` demo
#[function_component(UseInterval)]
pub fn interval() -> Html {
let millis = use_state(|| 0);
let state = use_state(|| 0);
let onstart = {
let millis = millis.clone();
Callback::from(move |_| millis.set(2000))
};
let oncancel = {
let millis = millis.clone();
Callback::from(move |_| millis.set(0))
};
{
let state = state.clone();
use_interval(
move || {
state.set(*state + 1);
},
*millis,
);
}
html! {
<div class="app">
<header class="app-header">
<div>
<button onclick={onstart}>{ "Start interval" }</button>
<button onclick={oncancel}>{ "Cancel interval" }</button>
<p>
<b>{ "Interval state: " }</b>
{ *state }
</p>
</div>
</header>
</div>
}
}
|
Beta Was this translation helpful? Give feedback.
The recommended approach is to use
gloo-timers
. At the moment, the top-level docs need improvement so you should look at the modules for what you want to use. As the name suggests,Timeout
issetTimeout
andInterval
issetInterval
.