gpt4 book ai didi

JavaScript:即使为 False,也不会停止(意外的 token 函数)

转载 作者:行者123 更新时间:2023-11-28 18:09:10 24 4
gpt4 key购买 nike

[更新:转到底部]我不知道我做错了什么。我首先调用了该函数:

var WelcomeBreak = document.addEventListener("keypress", WelcomeFour(event));

然后,我发起了它:

function WelcomeFour(e)
{
var x = e.keyCode || e.which;
if (x == 13)
{
WelcomeAnimation = false;
}
}

我看到它是这样完成的(或者在某种程度上像这样),但是当我这样做时,我得到了一个“意外的 token 函数”。我不知道我做错了什么。我对自己正在做的事情不是很熟悉,但我足够熟悉,知道 Javascript 中的东西是如何工作的。如果您需要的话,这是完整的代码:

Javascript

function Start()
{
var MainContainer = document.getElementsByClassName("Main_Container")[0];
var WelcomeParagraph = document.createElement("p");
var WelcomeSpan = document.createElement("span");
var WindowHalfWidth = window.innerWidth / 2;
var WindowHalfHeight = window.innerHeight / 2;
var WelcomeSpanHalfWidth = 414.453 / 2;
var WelcomeSpanHalfHeight = 120 / 2;
var WelcomeBreak = document.addEventListener("keypress", WelcomeFour(event));
var WelcomeAnimation = new Boolean(true);
do
{
WelcomeSpan.style.opacity = 0;
var Op1 = 0;
var Op2 = 1;
var foo1, foo2, foo3;
foo1 = setInterval(WelcomeOne, 20);
function WelcomeOne()
{
WelcomeSpan.style.opacity = Op1;
Op1 = Op1 + .01;
if (Op1 > 1)
{
clearInterval(foo1);
foo2 = setTimeout(WelcomeTwo, 1000);
}
}
function WelcomeTwo()
{
clearTimeout(foo2);
foo3 = setInterval(WelcomeThree, 20);
}
function WelcomeThree()
{
WelcomeSpan.style.opacity = Op2;
Op2 = Op2 - .01;
if (Op2 < 0)
{
clearInterval(foo3);
}
}
WelcomeAnimation = false;
}
function WelcomeFour(e)
{
var x = e.keyCode || e.which;
if (x == 13)
{
WelcomeAnimation = false;
}
}
while (WelcomeAnimation == true);
WelcomeParagraph.style.margin = "0%";
WelcomeParagraph.style.position = "absolute";
WelcomeParagraph.style.top = (WindowHalfHeight - WelcomeSpanHalfHeight) + "px";
WelcomeParagraph.style.left = (WindowHalfWidth - WelcomeSpanHalfWidth) + "px";
WelcomeSpan.style.fontSize = "100px";
WelcomeSpan.style.fontFamily = "Roboto";
WelcomeSpan.textContent = "Welcome";
WelcomeParagraph.appendChild(WelcomeSpan);
MainContainer.appendChild(WelcomeParagraph);
window.addEventListener("resize", Resize);
function Resize()
{
var WindowHalfWidth = window.innerWidth / 2;
var WindowHalfHeight = window.innerHeight / 2;
WelcomeParagraph.style.top = (WindowHalfHeight - WelcomeSpanHalfHeight) + "px";
WelcomeParagraph.style.left = (WindowHalfWidth - WelcomeSpanHalfWidth) + "px";
}
}

HTML

<html lang="en">
<head>
<!--
Name:Bradley
-->
<link href="Game1.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="http://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="Game1.js"></script>
<title>Game</title>
</head>

<body onload="Start()">
<div class="Main_Container">
</div>
</body>
</html>

CSS

html
{
margin:0%;
}
body
{
margin:0%;
overflow:hidden;
}
div.Main_Container
{
position:absolute;
height:100%;
width:100%;
overflow:hidden;
}

PS:非常感谢您对我应该如何开始编写代码的任何建议!我真的很想成为一名更好的编码员,所以任何事情都会有帮助:)

更新Javascript我将函数 WelcomeFour 移至 do block 中。它解决了部分问题,但不是全部。当我按回车键时,WelcomeFour 函数不会停止。我想我可以让它与 if 语句一起工作,但我不知 Prop 体如何。这是更新后的代码:

do
{
WelcomeSpan.style.opacity = 0;
var Op1 = 0;
var Op2 = 1;
var foo1, foo2, foo3;
foo1 = setInterval(WelcomeOne, 20);
function WelcomeOne()
{
WelcomeSpan.style.opacity = Op1;
Op1 = Op1 + .01;
if (Op1 > 1)
{
clearInterval(foo1);
foo2 = setTimeout(WelcomeTwo, 1000);
}
}
function WelcomeTwo()
{
clearTimeout(foo2);
foo3 = setInterval(WelcomeThree, 20);
}
function WelcomeThree()
{
WelcomeSpan.style.opacity = Op2;
Op2 = Op2 - .01;
if (Op2 < 0)
{
clearInterval(foo3);
}
}
function WelcomeFour(e)
{
var x = e.keyCode || e.which;
if (x == 13)
{
WelcomeAnimation = false;
}
}
WelcomeAnimation = false;
}
while (WelcomeAnimation == true);

最佳答案

您有一个do {} while () loop但您省略了 while () 部分并将函数声明放在其位置。

<小时/>
document.addEventListener("keypress", WelcomeFour(event));

您正在立即调用WelcomeFour,并尝试将其返回值指定为事件监听器。

不要调用它。

document.addEventListener("keypress", WelcomeFour);

关于JavaScript:即使为 False,也不会停止(意外的 token 函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41990207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com