在不使用JavaScript重新加载页面的情况下不断更改背景颜色
我在使用js更改背景颜色时遇到问题在不使用JavaScript重新加载页面的情况下不断更改背景颜色,javascript,html,css,Javascript,Html,Css,我在使用js更改背景颜色时遇到问题 <!DOCTYPE html> <html> <head> <title>First Js</title> </head> <body onload="init();"> </body> <script type="text/javascript"> function init() { var color = '#'+
<!DOCTYPE html>
<html>
<head>
<title>First Js</title>
</head>
<body onload="init();">
</body>
<script type="text/javascript">
function init() {
var color = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
document.body.style.backgroundColor = color;
}
</script>
</html>
我面临的问题是,每次我需要更改背景颜色时,我都需要重新加载它。有没有办法让它自动不断地改变自己
有没有办法让它自动不断地改变自己
您可以在init方法本身的末尾使用setTimeout调用init方法
<script type="text/javascript">
function init() {
var color = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
document.body.style.backgroundColor = color;
setTimeout( init, 5000 ); //once done, call it back again after 5 sec
}
init(); //call this once
</script>
有没有办法让它自动不断地改变自己
您可以在init方法本身的末尾使用setTimeout调用init方法
<script type="text/javascript">
function init() {
var color = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
document.body.style.backgroundColor = color;
setTimeout( init, 5000 ); //once done, call it back again after 5 sec
}
init(); //call this once
</script>
您可以这样做:
第一Js
函数初始化{
var color=+0x1000000+Math.random*0xffffff.toString16.substr1,6;
document.body.style.backgroundColor=颜色;
}
设置间隔函数{
初始化;
},1000;
您可以这样做:
第一Js
函数初始化{
var color=+0x1000000+Math.random*0xffffff.toString16.substr1,6;
document.body.style.backgroundColor=颜色;
}
设置间隔函数{
初始化;
},1000;