Uzair3D Posted August 2, 2015 Share Posted August 2, 2015 (edited) Please look at the code: in this line: line#42reactionTime = (clickedTime - startTime)/1000; all browsers are saying that startTime is not defined :/ any help will be appreciated <!doctype html><html><head> <title>Learning JavaScript</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type = "text/css"> #simpleBox{ width:200px; height:200px; background-color:red; display:none; } </style></head><body> <div id = "simpleBox"></div> <script type = "text/javascript"> function makeBox() { var clickedTime; var startTime; var reactionTime; var x = Math.random(); x = x*5000; setTimeout(function (){ document.getElementById("simpleBox").style.display = "block"; startTime = Date.now(); }, x); } document.getElementById("simpleBox").onclick = function (){ clickedTime = Date.now(); reactionTime = (clickedTime - startTime)/1000; alert(reactionTime); this.style.display = "none"; makeBox(); } makeBox(); </script></body></html> Edited August 2, 2015 by Uzair3D Link to comment Share on other sites More sharing options...
Don E Posted August 2, 2015 Share Posted August 2, 2015 Try removing var startTime out of the function makeBox and have it right before the makeBox function. Because it's in makeBox, the scope for that variable is local to the makeBox function only. 1 Link to comment Share on other sites More sharing options...
Uzair3D Posted August 2, 2015 Author Share Posted August 2, 2015 Try removing var startTime out of the function makeBox and have it right before the makeBox function. Because it's in makeBox, the scope for that variable is local to the makeBox function only. You are awesome. Thanks Link to comment Share on other sites More sharing options...
Don E Posted August 2, 2015 Share Posted August 2, 2015 Welcome. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now