JQuery Redirect to URL after specified time

前端 未结 9 965
感动是毒
感动是毒 2020-12-07 17:20

Is there a way to use JQuery to redirect to a specific URL after a give time period?

9条回答
  •  忘掉有多难
    2020-12-07 17:54

    This is improved @Vicky solution - it has clearInterval() added so it prevents a loop of reloading if the redirect takes too long:

                                $(document).ready(function () {
                                    var myTimer = window.setInterval(function () {
                                        var timeLeft = $("#countdown").html();
                                        if (eval(timeLeft) == 0) {
                                            console.log('Now redirecting');
                                            clearInterval(myTimer);
                                            window.location = ("@Html.Raw(HttpUtility.HtmlDecode(redirectUrl))");
                                        } else {
                                            $("#countdown").html(eval(timeLeft) - eval(1));
                                        }
                                    }, 1000);
                                });
    

提交回复
热议问题