Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不使用JavaScript重新加载页面的情况下不断更改背景颜色_Javascript_Html_Css - Fatal编程技术网

在不使用JavaScript重新加载页面的情况下不断更改背景颜色

在不使用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 = '#'+

我在使用js更改背景颜色时遇到问题

<!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;