router.push is not working inside settimeout
Unanswered
Entlebucher Mountain Dog posted this in #help-forum
Entlebucher Mountain DogOP
useEffect(() => {
const interval = setInterval(() => {
const now = new Date();
const difference = endDate.getTime() - now.getTime();
const d = Math.floor(difference / (1000 * 60 * 60 * 24));
const h = Math.floor(
(difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const m = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
const s = Math.floor((difference % (1000 * 60)) / 1000);
if (d <= 0 && h <= 0 && m <= 0 && s <= 0) {
router.push("/");
console.log("pushed!");
} else {
setDays(d);
setHours(h);
setMinutes(m);
setSeconds(s);
}
}, 1000);
return () => clearInterval(interval);
}, [endDate, router]);
const interval = setInterval(() => {
const now = new Date();
const difference = endDate.getTime() - now.getTime();
const d = Math.floor(difference / (1000 * 60 * 60 * 24));
const h = Math.floor(
(difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const m = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
const s = Math.floor((difference % (1000 * 60)) / 1000);
if (d <= 0 && h <= 0 && m <= 0 && s <= 0) {
router.push("/");
console.log("pushed!");
} else {
setDays(d);
setHours(h);
setMinutes(m);
setSeconds(s);
}
}, 1000);
return () => clearInterval(interval);
}, [endDate, router]);

25 Replies
Entlebucher Mountain DogOP
nope
i have a middleware btw
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
import { endDate } from "./constants";
export function middleware(req: NextRequest) {
const isDateEnded = endDate < new Date();
if (!isDateEnded) {
if (req.nextUrl.pathname === "/launch-timer") return NextResponse.next();
else return NextResponse.redirect(new URL("/launch-timer", req.url));
}
// if isDateEnded return true if user visit /launch-timer page should redirect to back
// if(isDateEnded){
// if(req.nextUrl.pathname === "/launch-timer") return NextResponse.redirect(new URL("/", req.url))
// }
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|icon.ico|banner.png|bd.svg|us.svg|hero-no-image.svg|logo.svg|map_logo.svg|not-found-product.jpg|product_details.svg|small_product.svg).*)"],
};
import { NextRequest } from "next/server";
import { endDate } from "./constants";
export function middleware(req: NextRequest) {
const isDateEnded = endDate < new Date();
if (!isDateEnded) {
if (req.nextUrl.pathname === "/launch-timer") return NextResponse.next();
else return NextResponse.redirect(new URL("/launch-timer", req.url));
}
// if isDateEnded return true if user visit /launch-timer page should redirect to back
// if(isDateEnded){
// if(req.nextUrl.pathname === "/launch-timer") return NextResponse.redirect(new URL("/", req.url))
// }
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|icon.ico|banner.png|bd.svg|us.svg|hero-no-image.svg|logo.svg|map_logo.svg|not-found-product.jpg|product_details.svg|small_product.svg).*)"],
};
after reloading the whole page then it was worked
@Entlebucher Mountain Dog nope
no error on browser console?
Entlebucher Mountain DogOP
no i didn't get any errors
do you see
pushed logged and does the url change?Entlebucher Mountain DogOP
yes the console.log is printing in browser console but not redirecting before reload the page
@Entlebucher Mountain Dog yes the console.log is printing in browser console but not redirecting before reload the page
does it work if you disable the middleware?
Entlebucher Mountain DogOP
lemme try
@Entlebucher Mountain Dog lemme try
just rename it to
_middleware.tsEntlebucher Mountain DogOP
i just comment the logic
yes now it's working
but the middleware also need for my requirments
is there any other way to achive it?
@Entlebucher Mountain Dog is there any other way to achive it?
what are you trying to achieve?
Entlebucher Mountain DogOP
case of using middleware
i'm trying to redirect the user when they trying to access /launch-timer page after endDate is not remain
i'm trying to redirect the user when they trying to access /launch-timer page after endDate is not remain
should i redirect the user from client side?
does this work?
const isDateEnded = endDate < new Date();
if (isDateEnded && req.nextUrl.pathname === '/launch-timer') {
return NextResponse.redirect(new URL("/", req.url));
}Entlebucher Mountain DogOP
it was work
sorry
lemme try
Thank you it's working now.

no prob