代码之家  ›  专栏  ›  技术社区  ›  aman

如何编写一个不冻结网站用户界面的延时?[复制品]

  •  -1
  • aman  · 技术社区  · 7 年前

    这个问题已经有了答案:

    我想在我的网站上延迟一段时间。到目前为止,我已经建立了一个睡眠功能,可以冻结我的系统数秒。但有了这个,我无法访问网站的用户界面。

    我的代码:

    <html>
    <head>
        <title></title>
    </head>
    <body>
        <button onclick="on_click()">Button-1</button>
        <button onclick="console.log('BYE');">Button-2</button>
        <script>
            function on_click()
            {
                sleep(15000);
                console.log("HI");
            }
            function sleep(delay)
            {
                var start = new Date().getTime();
                while (new Date().getTime() < start + delay);
            }
        </script>
    </body>
    </html>

    所以每当我点击 Button-1 ,我必须等待15秒才能单击 Button-2 . 我要我的程序允许我点击 按钮-1 它应该等待15秒,并在控制台上显示“嗨”。也允许我点击 按钮-2 . 请帮忙。谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Nadav    7 年前

    这个 setTimeout 函数允许您设置时间延迟,而不停止脚本运行正在进行的进程。