尝试多次使用超时功能

0 html javascript

我试图多次使用Timeout函数,但它只运行一次.我试图在6秒后获得第二个URL.

3秒后的第一个,这是正常的.还试过if else声明但没有效果.

<!doctype html>
<html>
<body>
    <p>You will be redirected in 3 seconds</p>
    <script>
        var time = setTimeout(function () {
            window.location = 'http://example.com'
        }, 3000);

        var one = setTimeout(function () {
            window.location = 'http://google.com'
        }, 6000);
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 9

JavaScript(在此上下文中)在网页内运行.

离开页面时,会破坏运行JavaScript的环境.

导航到其他页面后,您无法运行三个(或任何其他数量)秒的JavaScript函数.