javascript随机闪烁单词
这是我正在尝试制作的一个有趣的小脚本。将一个数组中的单词与另一个数组中的随机颜色一起闪烁。 (我主要考虑的是移动 bg 类型的交易。)
我在创建某种循环以导致单词“闪烁/更改”时遇到问题,到目前为止,它所做的只是在页面重新加载时进行更改。
*新* 好吧,我改变了它,所以现在它只是一个功能......而且它可以工作!但它似乎使用了浏览器内存或其他东西并崩溃了...哎呀...是否有一个清晰的内存或我应该使用的javascript的东西?
<html>
<head>
<style>
body
{
color:black;
}
#quotes
{
}
</style>
</head>
<body>
<script type="text/javascript">
function showQuote()
{
pickWords =
[
"Hi!",
"Welcome!",
"Hello!"
]
var word22 = pickWords[Math.floor(Math.random()*pickWords.length)];
pickColors =
[
"#aa2233",
"#00cc44",
"#F342AA"
]
var Color22 = pickColors[Math.floor(Math.random()*pickColors.length)];
var Top22 = (Math.floor(Math.random()*800));
var Left22 = (Math.floor(Math.random()*800));
var style33 = '<h4 style="padding-bottom:0px; padding-top:'+Top22+'px; padding-left:'+Left22+'px; font-size: 2.3em; color:'+Color22+';">';
var style34 = '</h4>';
var finWord22 = style33 + word22 + style34;
var duration = 400;
document.getElementById("quotes").innerHTML=finWord22;
setInterval('showQuote()',duration);
}
onload = function()
{
showQuote();
}
</script>
<div id="quotes"></div>
</body>
Here's a fun little script I'm trying to make. Flash words from an array with random colors from another array. (I'm mostly thinking about having a moving bg type deal.)
I'm having problems with creating some sort of loop to cause the words to "flash/change" so far all it does is change on page reload.
*new*
Well I changed it so now it is just one function... and it WORKS!! but it seems that it uses the browsers memory or something and crashes.... oops... is there a clear memory or something for javascript that I should use??
<html>
<head>
<style>
body
{
color:black;
}
#quotes
{
}
</style>
</head>
<body>
<script type="text/javascript">
function showQuote()
{
pickWords =
[
"Hi!",
"Welcome!",
"Hello!"
]
var word22 = pickWords[Math.floor(Math.random()*pickWords.length)];
pickColors =
[
"#aa2233",
"#00cc44",
"#F342AA"
]
var Color22 = pickColors[Math.floor(Math.random()*pickColors.length)];
var Top22 = (Math.floor(Math.random()*800));
var Left22 = (Math.floor(Math.random()*800));
var style33 = '<h4 style="padding-bottom:0px; padding-top:'+Top22+'px; padding-left:'+Left22+'px; font-size: 2.3em; color:'+Color22+';">';
var style34 = '</h4>';
var finWord22 = style33 + word22 + style34;
var duration = 400;
document.getElementById("quotes").innerHTML=finWord22;
setInterval('showQuote()',duration);
}
onload = function()
{
showQuote();
}
</script>
<div id="quotes"></div>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 showQuote() 函数内“pickword”。
现在,您在加载时选择一个单词,并在每次超时时使用该单词。
将整个代码包装到一个函数中并在加载时调用该函数。
You would need to 'pickword' inside the showQuote() function.
Right now, you are picking a word onload, and use that word on every timeout.
Wrap your whole code into a function and call that function on load.
您在函数中调用 setinterval,而您应该使用 settimeout。这应该可以帮助你解决崩溃问题:P
You're calling setinterval in the function, where as you should use settimeout. That should help you out with the crashing :P