Skip to content
Related Articles

Related Articles

JavaScript | Responsive time of an event
  • Last Updated : 19 Feb, 2019

Responsive time of an event is the time taken to load the result of that event and time taken to start that event. In this post, various shapes have been used using Math.random() function. The task is to find out the responsive time of changing these shapes.
As soon as you click the shape, it gets disappear and shows the response time on the top of the web page and a new shape get appear while the previous one gets disappeared.
JavaScript code to change the shapes and find their responsive time




<html>
  
<head>
    <style type="text/css">
        #s1 {
            height: 200px;
            width: 200px;
            border-radius: 50%;
            display: none;
        }
          
        #s2 {
            height: 200px;
            width: 200px;
            display: none;
        }
          
        #s3 {
            height: 140px;
            width: 200px;
            display: none;
        }
          
        #button {
            position: absolute;
            left: 100px;
            top: 100px;
            height: 70px;
            width: 300px;
            font-size: 25px;
            background-color: darkseagreen;
        }
    </style>
</head>
  
<body>
    <button id="button" onclick="myfun()">START GAME</button>
    <p id="time"></p>
    <div id="s1"></div>
    <div id="s2"></div>
    <div id="s3"></div>
    <script type="text/javascript">
        function myfun() {
            document.getElementById("button").style.display = "none";
            var shape = Math.floor((Math.random() * 3) + 1);
            var shape1 = "s" + shape;
            <!---Calculating random color--->
            <!---An array of 6 for HTML color code --->
            var a = new Array(6);
            var text = "";
            <!--- HTML color code begin with "# "--->
            text = text + "#";
            for (i = 0; i < 6; i++) {
                <!---To take values from 1 to 15 only--->
                a[i] = (Math.floor(Math.random() * 15) + 1);
                if (a[i] == 10) {
                    text = text + "A";
                } else if (a[i] == 11) {
                    text = text + "B";
                } else if (a[i] == 12) {
                    text = text + "C";
                } else if (a[i] == 13) {
                    text = text + "D";
                } else if (a[i] == 14) {
                    text = text + "E";
                } else if (a[i] == 15) {
                    text = text + "F";
                } else {
                    text = text + a[i];
                }
            }
  
            document.getElementById(shape1).style.backgroundColor = text;
            var a = document.getElementById(shape1);
            <!---Calculate the random location  where element has to be placed--->
            random1 = Math.floor(Math.random() * 250) + 1;
            random2 = Math.floor(Math.random() * 300) + 1;
            a.style.position = "absolute";
            a.style.left = random1;
            a.style.top = random2;
            document.getElementById(shape1).style.display = "block";
            var start = Date.now();
            document.getElementById(shape1).onclick = function() {
                var end = Date.now();
                document.getElementById(shape1).style.display = "none";
                var diff = end - start;
                var diff1 = diff / 600;
                document.getElementById("time").innerHTML = diff1;
                myfun();
            }
  
        }
    </script>
</body>
  
</html>

Output:
Before clicking the Start game button-

After clicking the Start game button-

On clicking the shape your response time and new shape will be appeared-

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :