-1

I want to make a time delay in my website. So far I have built a sleep function which can freeze my system for number of seconds. But with this, I'm not able to my access the User Interface of the website.

My Code:

<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>

So whenever I click Button-1, I'll have to wait for the 15 seconds to click the Button-2. I want my program to allow me to click on Button-1 and it should wait for 15 seconds and display "Hi" on the console. And also allowing me to click Button-2. Please Help. Thanks

aman
  • 307
  • 21
  • 48

1 Answers1

2

The setTimeout function allows you to set a time delay without stopping the script from running ongoing processes.

Nadav
  • 1,055
  • 1
  • 10
  • 23