【发布时间】:2016-08-15 06:48:39
【问题描述】:
这里是 Javascript 新手,经过数小时挖掘其他问题后,老实说,我不太确定如何解释,但我会尽力而为,希望您能帮助我。
HTML:
<div id='header'> <h1> Pastel Land </h1> </div>
<div id='container'>
<div id='readyContainer'>
<h3> This game will start in </h3>
<h1 id='readySeconds'> </h1>
</div>
<div id='shape'> </div>
</div>
<div id='features'>
<button id='start'> START </button>
<button id='stop'> STOP </button>
<p id='timeBox'></p>
<p id='timeAverageBox'></p>
</div>
<div id='testbox'> </div>
完整的脚本:
document.getElementById('start').onclick = function () {
document.getElementById('readyContainer').style.display = 'block';
document.getElementById('readySeconds').innerHTML = '3'
setTimeout(function() {document.getElementById('readySeconds').innerHTML = '2'}, 1000);
setTimeout(function() {document.getElementById('readySeconds').innerHTML = '1'}, 2000);
setTimeout(readyAlert,3000);
setTimeout(displayShape, 3000);
var style = document.getElementById('shape').style;
var el = document.getElementById('shape');
el.addEventListener("click", a, false);
el.addEventListener("click", b, false);
function a() {
style.display = "none";
displayShapeDelay(); // calls the delay function
}
function b() {
end = new Date().getTime(); // saves time when clicked
var time = (end - start)/1000 ; // calculates interval from shape creation until click
document.getElementById('timeBox').innerHTML = time + 's';
return time;
}
document.getElementById('testbox').innerHTML = b();
function readyAlert() {
document.getElementById('readyContainer').style.display = 'none';
}
function getRandomColor() {
var hex = ["#96ceb4", "#ffeead", "#ff6f69", "#ffcc5c", "#88db8b0", "#528491"];
var color = hex[Math.floor(Math.random() * 6)]; // generates integer numbers [0,5], selects indexed item from hex
return color;
}
function displayShape () {
var percentages = [];
for (var i=0; i<4; i++){ // generates a list with 4 different random integer values [5,60]
percentages.push((Math.floor(Math.random() * 61) + 5));
}
var width = (Math.floor(Math.random() * 61) + 5); // generates integer numbers [5,60]
var shapeRand = Math.random()
if (shapeRand < 0.3) { // circle
style.borderRadius = "50%";
} else if (shapeRand >= 0.3 && shapeRand < 0.6) { // random shape
style.borderTopLeftRadius = percentages[0] + "%";
style.borderBottomRightRadius = percentages[1] + "%";
style.borderTopRightRadius = percentages[2] + "%";
style.borderBottomLeftRadius = percentages[3] + "%";
} else { // square
style.borderRadius = "0%";
}
//general shape styles
style.width = width + "%";
style.height = width + "%";
style.display = "block";
style.backgroundColor = getRandomColor();
style.top = percentages[0] + "%";
style.left = percentages[3] + "%";
start = new Date().getTime(); // saves time when shape is created
console.log(width);
console.log(getRandomColor());
console.log(shapeRand);
console.log(percentages);
}
function displayShapeDelay () { // calls the main function with a delay between ]0s,2s[
setTimeout(displayShape, Math.random() * 2000);
}
document.getElementById('stop').onclick = function() {
}
}
在我有这个之前:
我的目标是将 var 'time' 返回到全局范围,以便我可以使用它来创建一个数组,其中包含每次单击创建的每个值。我已经意识到匿名函数不可能做到这一点。
document.getElementById('shape').onclick = function() { // calls the delay function
style.display = "none";
displayShapeDelay();
end = new Date().getTime();
time = (end - start)/1000 ;
document.getElementById('timeBox').innerHTML = time + 's';
return time
}
这就是我现在的代码:
var shapeClick = document.getElementById('shape');
shapeClick.addEventListener("click", a, false);
shapeClick.addEventListener("click", b, false);
function a() {
style.display = "none";
displayShapeDelay(); // calls the delay function
}
function b() {
end = new Date().getTime(); // saves time when clicked
var time = (end - start)/1000 ; // calculates interval from shape creation until click
document.getElementById('timeBox').innerHTML = time + 's';
return time;
}
document.getElementById('testbox').innerHTML = b();
现在,这里有几个问题:
1- 我似乎无法理解为什么在按下“开始”按钮后会为两个“时间 div”分配值。这意味着函数 b 正在运行,但它不应该只在 onClick 事件之后运行吗?
2- 在“第一轮”中,我理解为什么两个值都显示为 NaN,因为还没有为变量“time”分配值。但是在 onClick 事件执行后,在 'timeBox' 内分配的 'time' 值可以正常工作,但在函数外部调用的值却不行。函数b中的“返回时间”不是应该返回“时间”变量的值吗?
提前致谢!
【问题讨论】:
-
你能添加你的 HTML 吗?
-
1:b() 运行的原因是因为这一行“document.getElementById('testbox').innerHTML = b();”。它在我假设的全局范围内,所以当代码加载时 b() 会立即执行..你最好把它放在 b() 中,所以 "document.getElementById('testbox').innerHTML = time ;"
-
2: b() 返回时间,我假设您谈论的 testbox 元素没有随时间更新,那是因为 "document.getElementById('testbox').innerHTML = b ();"仅在初始加载时运行一次。当 b() 再次运行时它不会更新。看看我的其他评论会解决这个问题。
-
@whipdancer HTML 添加。
-
答案已更新以解决代码结构问题。发现 Pastel Land 有点像 Whac-a-Mole 街机游戏。 @whipdancer
标签: javascript function variables scope