You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently had a use case for a timeout that I could cancel, or run early. Specifically in my app it's a "redirecting" page, where there's a timeout of 3 seconds and a button to do the redirect immediately.
First off, is this possible with cancellablePromise or one of the other composables?
If not, maybe this would be a good addition? Somethings I added to it are:
SSR version for doing the timeout immediately on the server
Completed and cancelled refs
onUnmount cancel the timeout
Here's my implementation
import{ref,onBeforeUnmount}from"nuxt-composition-api";exportfunctionuseTimeout(ms: number|null,fn: ()=>void,opts: {enableSSRTimeout: boolean}){consttimeoutHandle=ref<any|undefined>(undefined);constcancelled=ref(false);constcomplete=ref(false);consttimeoutMs=process.client||opts?.enableSSRTimeout ? ms : null;/** * cancel the timeout * @returns whether the timeout was cancelled or not, if complete = true this will return false */constcancelTimeout=(): boolean=>{if(complete.value===true){returnfalse;}clearTimeout(timeoutHandle.value);cancelled.value=true;returntrue;};onBeforeUnmount(()=>cancelTimeout());constdoTimeout=(): void=>{complete.value=true;fn();};if(timeoutMs===null){doTimeout();}else{setTimeout(doTimeout,timeoutMs);}return{
timeoutHandle,
cancelled,
complete,
cancelTimeout
};}
Edit: removed ssrTimeout in favor of useTimeout handling it itself
The text was updated successfully, but these errors were encountered:
I recently had a use case for a timeout that I could cancel, or run early. Specifically in my app it's a "redirecting" page, where there's a timeout of 3 seconds and a button to do the redirect immediately.
First off, is this possible with cancellablePromise or one of the other composables?
If not, maybe this would be a good addition? Somethings I added to it are:
Here's my implementation
Edit: removed ssrTimeout in favor of useTimeout handling it itself
The text was updated successfully, but these errors were encountered: