JavaScript | Responsive time of an event
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-

Recommended Posts:
- Call multiple JavaScript functions in onclick event
- How to stop event propagation with inline onclick attribute in JavaScript ?
- How to remove time from date using JavaScript ?
- JavaScript | Program to generate one-time password (OTP)
- Print current day and time using HTML and JavaScript
- JavaScript | Call a function after a fixed time
- How to convert Unix timestamp to time in JavaScript?
- How to measure time taken by a function to execute using JavaScript?
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
- Best way to make a d3.js visualization layout responsive
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- preventDefault() Event Method
- HTML | DOM onkeypress Event
- HTML | ondblclick Event Attribute
- jQuery | Selectors and Event Methods
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



