gpt4 book ai didi

javascript - 你如何在 Javascript 中制作闪烁的动画?

转载 作者:行者123 更新时间:2023-11-30 18:00:19 25 4
gpt4 key购买 nike

我试图通过在眼睛的“0”和“_”之间交替来使一些 ascii 艺术闪烁。我粘贴了我目前在下面的代码。 ascii 人会简单地闭上眼睛,然后每隔几秒睁开一次。我不明白为什么我的代码不起作用,我也愿意使用其他方式。谢谢!

<script type="text/javascript">
function startBlinking() {
window.setInterval(blinking(),2000);
}

function blinking() {
var left=document.getElementById("leftEye");
var right=document.getElementbyId("rightEye");

left.innerHTML="_";
right.innerHTML="_";
window.setTimeout(function(){
left.innerHTML="0";
right.innerHTML="0";
},500);
}

startBlinking();
</script>

这是其余的代码!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Girl</title>
<style type="text/css">
pre {
font-family: monospace;
font-size: small;
}

</style>

<script type="text/javascript">

function startBlinking() {
window.setInterval(blinking(),2000);
}

function blinking() {
var left=document.getElementById("leftEye");
var right=document.getElementbyId("rightEye");

left.innerHTML="_";
right.innerHTML="_";
window.setTimeout(function(){
left.innerHTML="0";
right.innerHTML="0";
},500);
}

function buttonClicked() {
var lines=new Array();
lines=["You should not stay up so late...",
"Light roast coffee please.",
"Fire Emblem is so good.",
"How's the weather today?",
"I hope you studied for finals.",
"What else should I say?",
"No more ramen for dinner.",
"Placement text",
"Placement text",
"Placement text"];

var ran=Math.floor(Math.random()*10);
var parsedStr=parse(lines[ran]);
document.getElementById("text1").innerHTML=parsedStr[0];
document.getElementById("text2").innerHTML=parsedStr[1];
document.getElementById("text3").innerHTML=parsedStr[2];
}

function parse(str){
var length=str.length;
var strArray= new Array();
strArray=[" "," "," "];

if(length > 33) {
return parse("Too many characters!");
} else {
if(length<11) {
var spaces="";
for(var i=0; i<(11-length);i++) {
spaces+= " ";
}
strArray[0]=str.slice(0,length)+spaces;
} else if(length>11 && length <22) {
var spaces="";
for(var i=0; i<(22-length);i++) {
spaces+= " ";
}
strArray[0]=str.slice(0,11);
strArray[1]=str.slice(11,length)+spaces;
} else {
var spaces="";
for(var i=0; i<(33-length);i++) {
spaces+= " ";
}
strArray[0]=str.slice(0,11);
strArray[1]=str.slice(11,22);
strArray[2]=str.slice(22,length)+spaces;
}

return strArray;
}
}

startBlinking();
</script>
</head>
<body>
<pre>
_____ ,--------------,
/////\\ | <span id="text1">You should </span> |
///////\\ | <span id="text2">not stay up</span> |
\|<span id="leftEye">0</span> <span id="rightEye">0</span> \\\\ | <span id="text3">so late... </span> |
|| /||| &lt;,______________|
//\^_,|\\\
||+--| |--_|
|/ `-_-' \
/\/| V \_\
/ /\| __ |\ \
/ / ||_ |) | \ \
\ \ ||\\|__|__\ \
\ \ | \_________/
\ \| |
</pre>
<p><button type="button" id="talkButton" onclick="buttonClicked()">Talk</button></p>
</body>
</html>

最佳答案

您正在将 undefined 传递给 setInterval(因为那是 blinking 的返回值)。而是传递一个函数引用:

function startBlinking() {
window.setInterval(blinking, 2000);
//no parentheses here -----^
}

此外,请修正此拼写错误:getElementbyId 应为 getElementById(JavaScript 区分大小写)。并始终检查浏览器的错误控制台,你会发现这个错误的。

解决这两个问题后,它似乎可以工作:http://jsbin.com/ikubaz/1/edit

关于javascript - 你如何在 Javascript 中制作闪烁的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180178/

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