Created
January 7, 2013 04:02
-
-
Save santosh/4472207 to your computer and use it in GitHub Desktop.
JavaScript: Changes CSS value on a regular interval of time. This example will show how to change background color of body every 1 second
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html dir="ltr" lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title> Random Color Generator in JavaScript </title> | |
<script type="text/javascript" charset="utf-8"> | |
// generate random color | |
function colorname() { | |
return '#' + Math.floor(Math.random() * 0xFFFFFF << 0).toString(16); | |
} | |
// get rid of window.onload stuff by putting entire script in the body | |
window.onload = function() { | |
var everyOneSecond = function() { | |
// See https://developer.mozilla.org/en-US/docs/DOM for reference of DOM. | |
document.body.style.backgroundColor = colorname(); | |
}; | |
// time in milliseconds | |
setInterval(everyOneSecond, 1000) | |
}; | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another way to generate random colors: