Advertisement
katsiri

Javascript reaction tester project

Nov 7th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.13 KB | None | 0 0
  1. <!--    code for the Javascript reaction tester project
  2.      written according to The Complete Web Developer course of Udemy.com
  3. -->
  4.  
  5. <!doctype html>
  6.  
  7. <html>
  8.  
  9. <head>
  10.    
  11.     <title>JavaScript Reaction Test</title>
  12.  
  13.     <meta charset    = "utf-8" />
  14.     <meta http-equiv = "content-type" content="text/html; charset=utf-8" />
  15.     <meta name       = "viewport" content="width=device-width, initial-scale=1" />
  16.    
  17.     <style type="text/css">
  18.    
  19.         .redBox{
  20.        
  21.             width:200px;
  22.             height:200px;
  23.             background-color:red;
  24.             color:white;
  25.             text-align: center;
  26.             padding: 20px;
  27.             display:none;
  28.        
  29.         }
  30.    
  31.     </style>
  32.    
  33.    
  34. </head>
  35.  
  36. <body dir         = "auto" >
  37.  
  38.         <p>Your Time: <span id="time">0.0 </span> seconds!</p>
  39.  
  40.         <div class="redBox" id="magicBox">
  41.  
  42.             <h1>Click me </h1>
  43.  
  44.             <p>Your Time: <span id="showTime">0.0 </span> seconds!</p>     
  45.        
  46.         </div>
  47.    
  48.     <script type="text/javascript">
  49.    
  50.    
  51.         var clickedTime;
  52.         var createdTime;
  53.         var reactionTime;
  54.        
  55.         function getRandomColor(){
  56.            
  57.             var letters = '0123456789ABCDEF'.split('');
  58.             var color = '#';
  59.             for (var i=0; i<6; i++){
  60.                
  61.                 color+= letters[Math.round(Math.random() * 15)];
  62.             }
  63.            
  64.             return color;
  65.         }
  66.  
  67.         function makeBox(){
  68.  
  69.             var time=Math.random(); // a random number between 0 and 1
  70.  
  71.             time=time*3000;
  72.  
  73.             setTimeout(function(){
  74.                
  75.                 if (Math.random()>0.5){
  76.                
  77.                 document.getElementById("magicBox").style.borderRadius= "50%"
  78.                 }   else{
  79.                    
  80.                 document.getElementById("magicBox").style.borderRadius= "initial";
  81.                    
  82.                 }
  83.            
  84.             document.getElementById("magicBox").style.backgroundColor=getRandomColor();
  85.            
  86.             document.getElementById("magicBox").style.display="block";
  87.  
  88.             createdTime=Date.now();
  89.  
  90.             }, time);   // 3,000 milliseconds = 3 seconds
  91.  
  92.         }
  93.  
  94.         document.getElementById("magicBox").onclick=function(){
  95.  
  96.             clickedTime=Date.now();
  97.  
  98.             reactionTime=(clickedTime-createdTime)/1000;
  99.  
  100.             document.getElementById("time").innerHTML=reactionTime;
  101.            
  102.             document.getElementById("showTime").innerHTML=reactionTime;
  103.  
  104.             this.style.display="none";
  105.  
  106.             makeBox();
  107.            
  108.         }
  109.  
  110.         makeBox();         
  111.    
  112.     </script>
  113.    
  114. </body>
  115.  
  116. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement