JQuery Redirect to URL after specified time

前端 未结 9 960
感动是毒
感动是毒 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 18:06

    Yes, the solution is to use setTimeout, like this:

    var delay = 10000;
    var url = "https://stackoverflow.com";
    var timeoutID = setTimeout(function() {
        window.location.href = url;
    }, delay);
    

    note that the result was stored into timeoutID. If, for whatever reason you need to cancel the order, you just need to call

    clearTimeout(timeoutID);
    

提交回复
热议问题