用周期函数把一个date类的时分秒写入到 一个页面,时分秒在走动,实现时分秒字体颜色随机变化
<script>
window.onload = function() {
var clock = document.getElementById('clock');
var color = ['red', 'green', 'yellow', 'blue', 'black', 'gold', 'orange', 'gray', 'pink', 'maroon']; //钟表颜色数组
setInterval(function() {
var now = new Date();
clock.innerHTML = now.getFullYear() + '年' + (now.getMonth()+1) + '月' + now.getDate() + '日' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
var radom = Math.floor(Math.random() * color.length + 1) - 1; //随机bai数
clock.style.color = color[radom]; //设置随机颜色
}, 1000)
}
</script>
<body>
<span id = "clock" style="font-size:20px;"></span>
</body>