简体   繁体   English

继续更改背景颜色,而无需使用JavaScript重新加载页面

[英]Keep on changing background colour without reloading page using JavaScript

I'm facing a problem while changing the background colour using js. 我在使用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>

The problem I'm facing is that, everytime I need to change the background color I need to reload it. 我面临的问题是,每次需要更改背景颜色时,都需要重新加载它。 Is there any way by which it automatically keeps on changing itself. 有什么方法可以自动保持自身的变化。

Is there any way by which it automatically keeps on changing itself. 有什么方法可以自动保持自身的变化。

You can call init method using setTimeout by the end of init method itself 您可以在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>

No need to call it in body onload now. 现在无需在body onload调用它。

Demo 演示版

 function init() { var color = '#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6); document.body.style.backgroundColor = color; setTimeout(init, 5000); } init(); 

You can do like this: 您可以这样:

 <!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; } setInterval(function(){ init(); },1000); </script> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载Javascript(在后台)而无需重新加载页面 - Load Javascript (in background) without reloading the page 使用Javascript更改表格行的背景色 - Changing the background colour of table rows using Javascript 如何在不重新加载页面的情况下使用javascript来记录元素的高度 - How to keep recording the height of an element using javascript even without reloading the page 在 CSS 或 JavaScript 中不更改背景颜色的情况下将输入字段设为只读 - Make input field readonly without changing background colour in CSS or JavaScript 使用javascript重新加载div而不重新加载页面 - reload div without reloading page using javascript 使用Java脚本更改背景颜色并在刷新页面后保留 - Changing Background colour with Javascript and keeping it after refreshing the page 在不重新加载页面的情况下更改URL - Changing the URL without reloading the page 使用颜色选择器在屏幕上更改形状的颜色,而无需重新加载页面 - Changing color of a shape on screen using a color picker without reloading the page 在不使用alert()的情况下在后台重新加载页面(改为使用sweetalert) - Reloading page in background without alert() (using sweetalert instead) 显示而不重新加载页面javascript - reveal without reloading page javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM