javascript随机闪烁单词

发布于 2024-11-19 21:11:43 字数 1294 浏览 4 评论 0原文

这是我正在尝试制作的一个有趣的小脚本。将一个数组中的单词与另一个数组中的随机颜色一起闪烁。 (我主要考虑的是移动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-11-26 21:11:43

您需要在 showQuote() 函数内“pickword”。

现在,您在加载时选择一个单词,并在每次超时时使用该单词。

将整个代码包装到一个函数中并在加载时调用该函数。

function ShowQuote(){
    //...
    setTimeout(ShowQuote, duration);
}

ShowQuote();

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.

function ShowQuote(){
    //...
    setTimeout(ShowQuote, duration);
}

ShowQuote();
皇甫轩 2024-11-26 21:11:43

您在函数中调用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文