设置javascript随机化器的计时器
随机化器可以工作,但正在寻找一种方法,让它使用setInterval循环,这样它就可以在网站上自己的div中连续运行设置javascript随机化器的计时器,javascript,setinterval,Javascript,Setinterval,随机化器可以工作,但正在寻找一种方法,让它使用setInterval循环,这样它就可以在网站上自己的div中连续运行 <script language="JavaScript"> setInterval(quotes(), 3000); function quotes(i) { var r_text = new Array(); r_text[0] = "All the leaves are brown"; r_text[1] = "And the sky i
<script language="JavaScript">
setInterval(quotes(), 3000);
function quotes(i) {
var r_text = new Array();
r_text[0] = "All the leaves are brown";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7 * Math.random())
document.write(r_text[i]);
}
</script>
更改setIntervalquotes,3000;设置IntervalQuotes,3000 您需要修复setInterval调用函数
改变
setInterval(quotes(), 3000);
到
该函数将每3秒随机一次
编辑:此外,document.write已弃用,应替换为用于写入html的其他函数
第二次编辑:您还传递了一个从未使用过的参数。考虑删除它。
StStand需要一个函数调用,调用中的括号使它执行函数并将值返回到SET间隔。另外,您不需要i作为参数
尝试: setIntervalquotes,3000; 函数引号{ var r_text=新数组; r_text[0]=所有的叶子都是棕色的; r_text[1]=天空是灰色的; 我一直在散步; r_text[3]=在冬天的一天; r_text[4]=我会安全和温暖; r_text[5]=如果我在洛杉矶。; r_text[6]=加州梦,在这样一个冬天; var i=Math.floor7*Math.random 文件编写者/文本[i];} 您的设置间隔不正确
setInterval(function(){quotes()}, 3000);
function quotes() {
var r_text = new Array();
r_text[0] = "All the leaves are brown";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7 * Math.random())
document.write(r_text[i]);
}
这种方式将代码分成逻辑部分;有一个函数用于获取随机引用,在setInterval中有一个匿名函数用于document.write。你也可以使用数组速记来定义你的引号,而不是像其他答案那样,分别定义每个引号,然后每3秒钟重新定义一次
~123.45也有同样的斩断技巧:还有,将数组定义和函数名更改为动词的道具。函数名不应该是名词。我在这里做了几乎相同的事情@JacqueGoupil Nice!我想知道刚刚用chrome检查过的jsPerf是否有性能差异。>>0似乎比~~ x和Woah都快,我一直认为按位运算的速度会大大超过数学函数的速度。谢谢的确请记住OP,当您在函数名后面加上时,您正在调用该函数并返回它在被调用位置返回的任何内容。Set interval需要知道函数对象本身,它必须每3秒调用一次函数对象,该函数对象用括号表示。澄清得好@V.Rubinetti。感谢您扩展vs no。我认为无论何时学习新东西,都要有背景,这一点很重要,不仅仅是这样。只是一个便条,document.write并不是不受欢迎的。它已经存在了一段时间,在可预见的未来可能也会存在。然而,正如Steve所指出的,它可能不是您想要使用的。谢谢你纠正我的话。弃用是不正确的,但应该避免
setInterval(function(){quotes()}, 3000);
function quotes() {
var r_text = new Array();
r_text[0] = "All the leaves are brown";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7 * Math.random())
document.write(r_text[i]);
}
var quotes = [
"All the leaves are brown",
"And the sky is grey",
"I've been for a walk",
"On a winter's day",
"I'd be safe and warm",
"If I was in L.A.",
"California dreaming, On such a winter's day"
];
function getRandomQuote() {
var index = Math.random() * quotes.length << 0;
return quotes[index];
}
setInterval(function() {
var quote = getRandomQuote();
document.write(quote);
}, 3000);