Javascript 随机秒后更改方框颜色
我编写了一个java脚本代码:Javascript 随机秒后更改方框颜色,javascript,jquery,html,Javascript,Jquery,Html,我编写了一个java脚本代码: <html> <head> <style type="text/css"> #myDiv { height: 150px; width: 150px; background-color: red; } </style> </head> <body> <d
<html>
<head>
<style type="text/css">
#myDiv {
height: 150px;
width: 150px;
background-color: red;
}
</style>
</head>
<body>
<div id="myDiv"></div>
<script type="text/javascript">
function setColor(){
var x = '0123456789ABCDEF';
var y='#';
var i;
for(i=0; i<6; i++){
y = y+x[Math.round(Math.random()*15)];
}
document.getElementById("myDiv").style.backgroundColor=y;
}
var myVar = setInterval(function(){setColor()}, 1000);
</script>
</body>
</html>
#myDiv{
高度:150像素;
宽度:150px;
背景色:红色;
}
函数setColor(){
变量x='0123456789ABCDEF';
变量y='#';
var i;
对于(i=0;i首先,您不能像javascript中的数组那样只访问字符串,请改用.charAt()
方法
var hex = x.charAt(Math.round(Math.random()*15)),
color = '#' + hex + hex + hex;
document.getElementById("myDiv").style.backgroundColor=color;
首先,不能像javascript中的数组那样访问字符串,而是使用.charAt()
方法
var hex = x.charAt(Math.round(Math.random()*15)),
color = '#' + hex + hex + hex;
document.getElementById("myDiv").style.backgroundColor=color;
首先,不能像javascript中的数组那样访问字符串,而是使用.charAt()
方法
var hex = x.charAt(Math.round(Math.random()*15)),
color = '#' + hex + hex + hex;
document.getElementById("myDiv").style.backgroundColor=color;
首先,不能像javascript中的数组那样访问字符串,而是使用.charAt()
方法
var hex = x.charAt(Math.round(Math.random()*15)),
color = '#' + hex + hex + hex;
document.getElementById("myDiv").style.backgroundColor=color;
Javascript函数
此函数将为您提供以下功能:
function doSomething() {}
(function loop() {
var rand = Math.round(Math.random() * (3000 - 500)) + 500;
setTimeout(function() {
//alert('A');
doSomething();
loop();
}, rand);
}());
它将以随机间隔doSomething()
)
-只需从警报中删除“/”即可看到它的作用!:)
以你为例
这将使用您的函数更改div的颜色:)
编辑
解释
第一行:(获取兰德)
第二行(setTimeout)
循环()
Javascript函数
此函数将为您提供以下功能:
function doSomething() {}
(function loop() {
var rand = Math.round(Math.random() * (3000 - 500)) + 500;
setTimeout(function() {
//alert('A');
doSomething();
loop();
}, rand);
}());
它将以随机间隔doSomething()
)
-只需从警报中删除“/”即可看到它的作用!:)
以你为例
这将使用您的函数更改div的颜色:)
编辑
解释
第一行:(获取兰德)
第二行(setTimeout)
循环()
Javascript函数
此函数将为您提供以下功能:
function doSomething() {}
(function loop() {
var rand = Math.round(Math.random() * (3000 - 500)) + 500;
setTimeout(function() {
//alert('A');
doSomething();
loop();
}, rand);
}());
它将以随机间隔doSomething()
)
-只需从警报中删除“/”即可看到它的作用!:)
以你为例
这将使用您的函数更改div的颜色:)
编辑
解释
第一行:(获取兰德)
第二行(setTimeout)
循环()
Javascript函数
此函数将为您提供以下功能:
function doSomething() {}
(function loop() {
var rand = Math.round(Math.random() * (3000 - 500)) + 500;
setTimeout(function() {
//alert('A');
doSomething();
loop();
}, rand);
}());
它将以随机间隔doSomething()
)
-只需从警报中删除“/”即可看到它的作用!:)
以你为例
这将使用您的函数更改div的颜色:)
编辑
解释
第一行:(获取兰德)
第二行(setTimeout)
循环()
非常感谢,它工作得很好。但对我来说,它很难理解。你能解释一下它的算法吗。它是如何工作的?请!!!!!请看编辑。非常感谢,它工作得很好。但对我来说,它很难理解。你能解释一下它的算法吗。它是如何工作的?请!!!!!!!请看编辑。非常感谢你,它工作得很好。但对我来说,它很难理解。你能解释一下它的算法吗。它是如何工作的吗?请!!!!!请看编辑。非常感谢你,它工作得很好。但对我来说,它很难理解。你能解释一下它的算法吗thm。它是如何工作的?请!!!!!请看编辑。几乎它是一个