Repository navigation
|
I try to use background loop like setInterval and clearInterval. Please advise and help. |
Answered by
ranile
Dec 26, 2021
Replies: 2 comments
|
The recommended approach is to use |
0 replies
Answer selected by
ranile
|
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>
}
}
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,TimeoutissetTimeoutandIntervalissetInterval.